std.encoding
Encodes bytes as text and back: Base64 and Base64Url (RFC 4648 §4, §5), Hex (RFC 4648
§8) and Utf16 (RFC 2781), plus reading and writing fixed-width integers as bytes, either end
first. Each encoding is a struct of static members used as a name space:
Base64.encode(bytes).
This module handles bytes and text only; writing the shape of a value — its fields, variants
and elements — into a format such as JSON is std.codec's job.
The decoders are strict, as RFC 4648 §3.3 and §3.5 allow: Base64 requires its padding and zero bits past the last byte, and accepts no whitespace. Input that is not the encoding throws an EncodingError at the first offset that is wrong.
Examples
import std.io { println };
import std.encoding { Base64, Hex, EncodingError };
fn main(): void throws EncodingError {
let bytes = "Lyric".bytes();
println(Base64.encode(bytes));
println(Hex.encode(bytes));
let back = try Base64.decode("THlyaWM=");
println(back.length());
}
THlyaWM=
4c79726963
5
Types
EncodingError struct — The error thrown for text that is not the encoding it was decoded as: where — a byte of the text, a unit of UTF-16 — and what is wrong there.
Base64Url struct — Base64 with the URL- and file-safe alphabet (RFC 4648 §5),
- and _, padded as Base64 is.Hex struct — Hexadecimal, two digits per byte:
encode writes small letters, decode reads either case.Extensions
extend int8
pub static fn fromBytesLE(s: Slice<uint8>): int8
pub static fn fromBytesBE(s: Slice<uint8>): int8
pub fn toBytesLE(): uint8[1]
pub fn toBytesBE(): uint8[1]
extend int16
pub static fn fromBytesLE(s: Slice<uint8>): int16
pub static fn fromBytesBE(s: Slice<uint8>): int16
pub fn toBytesLE(): uint8[2]
pub fn toBytesBE(): uint8[2]
extend int32
pub static fn fromBytesLE(s: Slice<uint8>): int32
pub static fn fromBytesBE(s: Slice<uint8>): int32
pub fn toBytesLE(): uint8[4]
pub fn toBytesBE(): uint8[4]
extend int
pub static fn fromBytesLE(s: Slice<uint8>): int
pub static fn fromBytesBE(s: Slice<uint8>): int
pub fn toBytesLE(): uint8[8]
pub fn toBytesBE(): uint8[8]
extend uint8
pub static fn fromBytesLE(s: Slice<uint8>): uint8
pub static fn fromBytesBE(s: Slice<uint8>): uint8
pub fn toBytesLE(): uint8[1]
pub fn toBytesBE(): uint8[1]
extend uint16
pub static fn fromBytesLE(s: Slice<uint8>): uint16
pub static fn fromBytesBE(s: Slice<uint8>): uint16
pub fn toBytesLE(): uint8[2]
pub fn toBytesBE(): uint8[2]
extend uint32
pub static fn fromBytesLE(s: Slice<uint8>): uint32
pub static fn fromBytesBE(s: Slice<uint8>): uint32
pub fn toBytesLE(): uint8[4]
pub fn toBytesBE(): uint8[4]
extend uint
pub static fn fromBytesLE(s: Slice<uint8>): uint
pub static fn fromBytesBE(s: Slice<uint8>): uint
pub fn toBytesLE(): uint8[8]
pub fn toBytesBE(): uint8[8]
extend Slice<uint8>
pub fn getUint16LE(at: int): uint16
pub fn getUint16BE(at: int): uint16
pub fn getUint32LE(at: int): uint32
pub fn getUint32BE(at: int): uint32
pub fn getUint64LE(at: int): uint
pub fn getUint64BE(at: int): uint
pub fn getInt16LE(at: int): int16
pub fn getInt16BE(at: int): int16
pub fn getInt32LE(at: int): int32
pub fn getInt32BE(at: int): int32
pub fn getInt64LE(at: int): int
pub fn getInt64BE(at: int): int