Functions
crc32
fn (data: Bytes | Str | Vec): Int
Calculate CRC32 checksum (IEEE polynomial). Used by protocols like AWS Event Stream for integrity checks.
Example
crc32("hello") // 907060870
crc32(Bytes([72, 101, 108, 108, 111])) // 907060870 (same)
// Verify message integrity
message-crc crc32(message-bytes)
eq(message-crc, expected-crc)
from-int
fn (value: Int, size: Int): Bytes
fn (value: Int, size: Int, endian: Str): Bytes
Convert an integer to bytes with specified size (1-8 bytes). Default is big-endian (network byte order).
Example
// Big-endian (default)
from-int(256, 2) // Bytes([1, 0])
from-int(16, 4) // Bytes([0, 0, 0, 16])
// Little-endian
from-int(256, 2, "le") // Bytes([0, 1])
to-int
fn (bytes: Bytes): Int
fn (bytes: Bytes, endian: Str): Int
Convert bytes to a signed integer. Default is big-endian (network byte order). Supports 1-8 bytes.
Example
// Big-endian (default)
to-int(Bytes([0, 0, 0, 16])) // 16
to-int(Bytes([0, 16])) // 16
// Little-endian
to-int(Bytes([16, 0, 0, 0]), "le") // 16
to-uint
fn (bytes: Bytes): Int
fn (bytes: Bytes, endian: Str): Int
Convert bytes to an unsigned integer. Default is big-endian. Supports 1-8 bytes. Values > i64::MAX will wrap to negative.
Example
// Unsigned interpretation
to-uint(Bytes([255, 255])) // 65535 (not -1)
to-vec
fn (bytes: Bytes): Vec
Convert bytes to a vector of integers for easier manipulation.
Example
data Bytes([1, 2, 3])
to-vec(data) // [1, 2, 3]