Encodings for MoonBit: base16, base32, base36, base58, base62 and base64, each with the same five faces — encode, encode_bytes, decode, decode_bytes and decode_lossy — and failures that name where they happened.
@base64.encode(data[:]) // "bW9vbmJhc2U="
@base64.encode(data[:], kind=Url, padding=false) // "bW9vbmJhc2U"
@base64.decode("bW9vbmJhc2U=") // raises on bad input
@base64.decode_lossy(pem_body) // skips newlines, never fails
@base64.encode_bytes(data[:]) // ASCII bytes, no String on the way{ "deps": { "moonbitstack/moonbase": "0.4.0" } }{ "import": ["moonbitstack/moonbase/base64"] }| Face | For |
|---|---|
| encode(input : BytesView, …) -> String | the ordinary case |
| encode_bytes(input : BytesView, …) -> Bytes | wire formats that hold bytes — an HTTP header, a gRPC metadata value — without a round trip through String |
| decode(input : StringView, …) -> Bytes raise Malformed | input you want checked |
| decode_bytes(input : BytesView, …) -> Bytes raise Malformed | the same, for callers who never had a string |
| decode_lossy(input : StringView, …) -> Bytes | a PEM body full of newlines, a fingerprint full of colons, a token with spaces in it |
@base32.encode(data[:], kind=Hex) // RFC 4648 §7, sort-order preserving
@base16.encode(data[:], kind=Upper) // 666F6F
@base64.encode(data[:], padding=false) // no trailing '='@base62.encode_int(123456789) // "8M0kX"
@base62.decode_int("8M0kX") // 123456789try @base64.decode("Zm*vYg==") catch {
Bad(at~, char~) => … // at = 2, char = '*'
Truncated(at~) => … // the input ends mid-group
Padding(at~) => … // '=' where it cannot be
Checksum(at~) => … // for the encodings that carry one
}| Package | Specification | Notes |
|---|---|---|
| base16 | RFC 4648 §8 | byte-aligned, so leading zeros survive; encode_colons writes fingerprints |
| base32 | §6, §7 | kind=Hex keeps byte order under sorting |
| base36 | multibase k/K | numbers and identifiers |
| base58 | Bitcoin | no 0, O, I or l; leading zero bytes stay as leading 1 |
| base62 | — | digits, upper, lower; short identifiers |
| base64 | §4, §5 | kind=Url for tokens and file names; decode_any reads either alphabet |
let crockford = @moonbase.Alphabet::new("0123456789ABCDEFGHJKMNPQRSTVWXYZ")
@moonbase.encode(data[:], crockford)moon run examples/tour --target wasm-gcmoon check --target all --deny-warn
moon test --target allInstall
Download zipEncodings for MoonBit: base16, base32, base36, base58, base62 and base64, each with the same five faces — encode, encode_bytes, decode, decode_bytes and decode_lossy — and failures that name where they happened.