Functions
Standard-alphabet base64 with '=' padding.
s: Bytes to encode.
let encoded = std.encoding.base64_encode("moon")
println(encoded) // bW9vbg==
println(std.encoding.base64_decode(encoded)) // moon
match std.encoding.try_base64_decode("not base64") {
ok(value) => println(value),
err(failure) => println(failure.message),
}Decodes padded standard-alphabet base64; errors on malformed input.
s: Padded standard-alphabet text to decode.
base64_decode as a value: malformed input is err.Encoding, not a terminated process. Decoders run on input from outside the program, so the sentinel form is the wrong default for anything holding a request.
s: Padded standard-alphabet text to decode.
URL-safe-alphabet ('-'/'_') base64 with '=' padding.
s: Bytes to encode.
let padded = std.encoding.base64_url_encode("moon?>")
println(std.encoding.base64_url_decode(padded))
println(std.encoding.try_base64_url_decode(padded).unwrap())
let token = std.encoding.base64_raw_url_encode("moon?>")
println(std.encoding.try_base64_raw_url_decode(token).unwrap())Decodes padded URL-safe base64; errors on malformed input.
s: Padded URL-safe text to decode.
base64_url_decode as a value. See try_base64_decode.
s: Padded URL-safe text to decode.
URL-safe base64 with the padding left off — Go's base64.RawURLEncoding, and what a value that travels in a URL path is usually spelled as.
s: Bytes to encode.
The strict decoder for the above, and strict on purpose: it rejects padding, the standard alphabet, and a length ≡ 1 (mod 4), because a permissive one resolves two spellings of an identifier to one value. That matters wherever the identifier names a resource — two URLs that should be distinct stop being.
Result-returning only. Input in this shape comes from outside the program by definition, so a sentinel form would be the wrong default (see try_base64_decode).
s: Unpadded URL-safe text; padding, the standard alphabet, and a length ≡ 1 (mod 4) are all rejected.
Lowercase hex, two digits per byte.
s: Bytes to encode.
let encoded = std.encoding.hex_encode("moon")
println(encoded) // 6d6f6f6e
println(std.encoding.hex_decode(encoded)) // moon
println(std.encoding.try_hex_decode("xyz").is_err())Decodes hex (either case); errors on odd length or an invalid digit.
s: Hex text of even length, in either case.
hex_decode as a value. See try_base64_decode.
s: Hex text of even length, in either case.