Invisible link to canonical for Microformats

bytes (slug)


slug.bytes

slug.bytes — byte buffer utilities

Conversions between @bytes, @str, hex strings, base64, and numeric lists. All functions are pure and operate on immutable byte values.

Byte literals use the 0x"<hex>" syntax, e.g. 0x"ff0a".

TOC

Functions

base64Decode(s)

fn slug.bytes#base64Decode(@str s) -> @bytes

decodes a standard base64 string to a byte buffer.

Parameter Type Default
s @str

Examples

base64Decode("")  // => 0x""
base64Decode("aGVsbG8gc2x1Zw==")  // => 0x"68656c6c6f20736c7567"

base64Encode(b)

fn slug.bytes#base64Encode(@bytes b) -> @str

encodes a byte buffer as a standard base64 string.

Parameter Type Default
b @bytes

Examples

base64Encode(0x"")  // => ""
base64Encode(0x"68656c6c6f20736c7567")  // => "aGVsbG8gc2x1Zw=="

bytesToHexStr(b)

fn slug.bytes#bytesToHexStr(@bytes b) -> @str

encodes a byte buffer as a lowercase hex string.

Parameter Type Default
b @bytes

Examples

bytesToHexStr(0x"")  // => ""
bytesToHexStr(0x"68656c6c6f20736c7567")  // => "68656c6c6f20736c7567"

bytesToNumbers(b, i, acc)

fn slug.bytes#bytesToNumbers(@bytes b, i = 0, acc = []) -> @list

converts a byte buffer to a list of numeric byte values (0–255).

Parameter Type Default
b @bytes
i   0
acc   []

Examples

bytesToNumbers(0x"a8ff04")  // => [168, 255, 4]

bytesToStr(b)

fn slug.bytes#bytesToStr(@bytes b) -> @str

converts a byte buffer to a UTF-8 string.

Parameter Type Default
b @bytes

Examples

bytesToStr(0x"")  // => ""
bytesToStr(0x"68656c6c6f20736c7567")  // => "hello slug"

hexStrToBytes(hex)

fn slug.bytes#hexStrToBytes(@str hex) -> @bytes

decodes a lowercase hex string to a byte buffer.

Parameter Type Default
hex @str

Examples

hexStrToBytes("")  // => 0x""
hexStrToBytes("68656c6c6f20736c7567")  // => 0x"68656c6c6f20736c7567"
hexStrToBytes("a8ff04")  // => 0x"a8ff04"

repeat(b, count, acc)

fn slug.bytes#repeat(@bytes b, @num count, @bytes acc = 0x"") -> @bytes

repeats a byte buffer count times and returns the concatenated result.

Parameter Type Default
b @bytes
count @num
acc @bytes 0x""

Examples

repeat(0x"ff", 3)  // => 0x"ffffff"

strToBytes(s)

fn slug.bytes#strToBytes(@str s) -> @bytes

converts a UTF-8 string to a byte buffer.

Parameter Type Default
s @str

Examples

strToBytes("")  // => 0x""
strToBytes("hello slug")  // => 0x"68656c6c6f20736c7567"