Skip to content

Hexadecimal ​

Signaturehex.encode(bytes: u8[]): string
Signaturehex.decode(text: string): Result<u8[], EncodingError>

Import hex from tyrus:encoding. encode(bytes) produces the canonical lowercase hexadecimal representation of the byte array, without an 0x prefix. decode(text) accepts only complete pairs of ASCII hexadecimal digits; odd length, an 0x prefix, whitespace, and invalid characters return Err(EncodingError). The decoder accepts only the lowercase canonical alphabet, so uppercase digits are rejected instead of silently normalized.

tyrus
import { EncodingError, hex } from "tyrus:encoding";

const bytes: u8[] = [0xde, 0xad, 0xbe, 0xef];
const encoded: string = hex.encode(bytes);
print(encoded); // deadbeef

try {
    const decoded: u8[] = hex.decode(encoded);
    print(decoded); // [222, 173, 190, 239]
} catch (error: EncodingError) {
    print(error.code);
}

Each pair of hexadecimal digits names exactly one byte, so the result carries no endianness: it is the direct base-16 spelling of a byte array.