Standard Library
std.encoding
Binary-to-text encodings: base64, URL-safe base64, and hex.
Exported functions
Encode bytes as standard base64 text.
Examples
use encoding from "std.encoding"
@println(encoding.base64_encode([102, 111, 111])) # Zm9vEncode UTF-8 text as standard base64 text.
Examples
use encoding from "std.encoding"
@println(encoding.base64_encode_text("hello")) # aGVsbG8=Decode standard base64 text into bytes, throwing on invalid input.
Examples
use encoding from "std.encoding"
def demonstrate() -> Void
try encoding.try_base64_decode("!!!") catch err
case is encoding.EncodingError.InvalidBase64
@println(`Invalid input: ${err.input}`)
return
case _
@panic("unexpected encoding error")
end
return
end
demonstrate()Decode standard base64 text into bytes, panicking on invalid input.
Examples
use encoding from "std.encoding"
const encoded = encoding.base64_encode_text("foo")
@println(encoding.base64_decode(encoded)) # [102, 111, 111]Encode bytes as URL-safe base64 text without padding.
Examples
use encoding from "std.encoding"
@println(encoding.base64url_encode([251, 255])) # -_8Encode UTF-8 text as URL-safe base64 text without padding.
Examples
use encoding from "std.encoding"
@println(encoding.base64url_encode_text("hello")) # aGVsbG8Decode URL-safe base64 text into bytes, throwing on invalid input.
Examples
use encoding from "std.encoding"
def demonstrate() -> Void
try encoding.try_base64url_decode("!!!") catch err
case is encoding.EncodingError.InvalidBase64
@println(`Invalid input: ${err.input}`)
return
case _
@panic("unexpected encoding error")
end
return
end
demonstrate()Decode URL-safe base64 text into bytes, panicking on invalid input.
Examples
use encoding from "std.encoding"
@println(encoding.base64url_decode("-_8")) # [251, 255]Encode bytes as lowercase hexadecimal text.
Examples
use encoding from "std.encoding"
@println(encoding.hex_encode([0, 255, 16])) # 00ff10Encode UTF-8 text as lowercase hexadecimal text.
Examples
use encoding from "std.encoding"
@println(encoding.hex_encode_text("foo")) # 666f6fDecode hexadecimal text into bytes, throwing on invalid input.
Examples
use encoding from "std.encoding"
def demonstrate() -> Void
try encoding.try_hex_decode("zz") catch err
case is encoding.EncodingError.InvalidHex
@println(`Invalid input: ${err.input}`)
return
case _
@panic("unexpected encoding error")
end
return
end
demonstrate()Decode hexadecimal text into bytes, panicking on invalid input.
Examples
use encoding from "std.encoding"
@println(encoding.hex_decode("00FF10")) # [0, 255, 16]