basex
base16 · base32 · base36 · base58 · base62 · base64 for MoonBit — the RFC 4648 byte codecs plus a generic base-N core behind base36/base58/base62.
moon add Lfan-ke/basex✦ Try it live
Type anything — encoded in the browser with the exact algorithms this library ships.
@basex
Generic positional base-N core. base36/base58/base62 pin an alphabet onto it.
struct Alphabet
An ordered set of digit characters defining a positional numeral system. The character at index i is the digit of value i, so the first character is always the zero digit. Build one with [Alphabet::new] and hand it to [encode] / [decode]; the base58 and base62 packages are thin wrappers that pin a specific alphabet.
fn Alphabet::new(chars : String) -> Alphabet
Build an [Alphabet] from its ordered digit characters, e.g. Alphabet::new("0123456789abcdef") for lowercase hexadecimal. Aborts when a character repeats or is not ASCII, since either makes the mapping ambiguous.
fn Alphabet::base(self : Alphabet) -> Int
The radix of this alphabet — the number of distinct digits.
fn Alphabet::char_of(self : Alphabet, v : Int) -> Char
The character for digit value v, where 0 <= v < base.
fn Alphabet::value_of(self : Alphabet, c : Char) -> Int?
The digit value of c in this alphabet, or None if c is not one of its characters.
fn encode(input : Bytes, alphabet : Alphabet) -> String
Encode raw bytes to a string over alphabet. Leading zero bytes map to leading zero-digit characters; the rest is read as a big-endian integer and rewritten in the target base. Round-trips with [decode].
fn decode(input : String, alphabet : Alphabet) -> Bytes?
Decode a string over alphabet back to the original bytes. Returns None if input holds any character outside alphabet. Round-trips with [encode].
@base16
RFC 4648 hexadecimal — byte-oriented (two chars per byte).
fn encode(input : Bytes) -> String
Encode bytes as lower-case hexadecimal (RFC 4648 §8). Each byte becomes exactly two characters, so a leading 0x00 is kept as "00" — unlike a big-integer base conversion, which drops leading zeros. Round-trips with [decode].
fn encode_upper(input : Bytes) -> String
Encode bytes as upper-case hexadecimal — the canonical spelling in RFC 4648. Round-trips with [decode], which accepts either case.
fn decode(input : String) -> Bytes?
Decode a hexadecimal string back to bytes. Upper- and lower-case digits are both accepted. Returns None when the length is odd or a character is not a hex digit, so it doubles as validation. Round-trips with [encode].
@base32
RFC 4648 base32 (A-Z2-7) and base32hex (0-9A-V).
fn encode(input : Bytes) -> String
Encode bytes as standard base32 (RFC 4648 §6): the A-Z2-7 alphabet, padded with = to a multiple of eight characters. Round-trips with [decode].
fn encode_hex(input : Bytes) -> String
Encode bytes as base32hex (RFC 4648 §7): the extended-hex 0-9A-V alphabet, which preserves sort order. Padded with =. Round-trips with [decode_hex].
fn decode(input : String) -> Bytes?
Decode a standard base32 string (RFC 4648 §6). Trailing = padding is optional. Returns None on an out-of-alphabet character or an impossible length. Round-trips with [encode].
fn decode_hex(input : String) -> Bytes?
Decode a base32hex string (RFC 4648 §7, 0-9A-V). Padding is optional. Round-trips with [encode_hex].
@base36
Dense case-insensitive 0-9a-z over the base-N core, with an integer mode.
let alphabet : @basex.Alphabet = @basex.Alphabet::new(
The base36 alphabet: the decimal digits followed by the lower-case letters. base36 is the densest encoding that stays case-insensitive and purely alphanumeric, which is why it is a common choice for human-facing short ids.
fn encode(input : Bytes) -> String
Encode bytes as a base36 string. Round-trips with [decode].
fn decode(input : String) -> Bytes?
Decode a base36 string back to bytes, or None on a character outside the alphabet. Round-trips with [encode].
fn encode_uint(value : UInt64) -> String
Encode an unsigned integer as base36 — the same text JavaScript's n.toString(36) produces (123456789 becomes "21i3v9"). Zero encodes to "0". Round-trips with [decode_uint].
fn decode_uint(input : String) -> UInt64?
Decode a base36 string produced by [encode_uint] back to an unsigned integer. Returns None for an empty string or a character outside the alphabet. Values beyond UInt64::max_value wrap, so keep ids within range.
@base58
The Bitcoin / IPFS alphabet over the base-N core.
let alphabet : @basex.Alphabet = @basex.Alphabet::new(
The base58 alphabet used by Bitcoin and IPFS. It leaves out the four characters that are easy to confuse by eye — 0 (zero), O, I, and l — so an encoded string survives being read aloud or copied by hand.
fn encode(input : Bytes) -> String
Encode bytes as a base58 string. Round-trips with [decode].
fn decode(input : String) -> Bytes?
Decode a base58 string back to bytes, or None when it holds a character outside the alphabet.
@base62
URL-safe 0-9A-Za-z over the base-N core, with an integer mode.
let alphabet : @basex.Alphabet = @basex.Alphabet::new(
The standard base62 alphabet: the decimal digits, then the upper-case letters, then the lower-case letters. Every character is URL-safe, which is why base62 is the usual choice for shortened links and opaque public ids.
fn encode(input : Bytes) -> String
Encode bytes as a base62 string. Round-trips with [decode].
fn decode(input : String) -> Bytes?
Decode a base62 string back to bytes, or None when it holds a character outside the alphabet.
fn encode_uint(value : UInt64) -> String
Encode an unsigned integer as base62 — the compact form a URL shortener gives a numeric id (123456789 becomes "8M0kX"). Zero encodes to "0". Round-trips with [decode_uint].
fn decode_uint(input : String) -> UInt64?
Decode a base62 string produced by [encode_uint] back to an unsigned integer. Returns None for an empty string or an out-of-alphabet character. Values beyond UInt64::max_value wrap, so keep ids within range.
@base64
RFC 4648 standard (+/) and URL-safe (-_) base64.
fn encode(input : Bytes) -> String
Encode bytes as standard base64 (RFC 4648 §4): the +/ alphabet, padded with = to a multiple of four characters. Round-trips with [decode].
fn encode_url(input : Bytes) -> String
Encode bytes as URL-safe base64 (RFC 4648 §5): the -_ alphabet with no padding, so the result is safe inside URLs and file names. Round-trips with [decode_url].
fn decode(input : String) -> Bytes?
Decode a standard base64 string (RFC 4648 §4). Trailing = padding is optional. Returns None on any character outside the alphabet or an impossible length. Round-trips with [encode].
fn decode_url(input : String) -> Bytes?
Decode a URL-safe base64 string (RFC 4648 §5, -_). Padding is optional. Round-trips with [encode_url].