Module: encoding.hex

Source: ./encoding/hex.reef


Overview

encoding/hex - Hexadecimal encoding and decoding

Provides functions to encode binary data to hexadecimal strings and decode hexadecimal strings back to binary data.

The decode functions (hex_decode, hex_to_byte, hex_decode_bytes) return core.result.Result, not a bare value: Ok on success (empty input "" is valid hex and decodes to Ok("")), Err(core.error.Error) on odd length or an invalid hex character.

Usage: import encoding.hex import core.result as result let encoded = hex_encode("Hello") // "48656c6c6f" let decoded = result.unwrap_ok(hex_decode("48656c6c6f")) // "Hello"


Functions

fn get_hex_lower(): string

Hex digit lookup strings

fn get_hex_upper(): string

fn hex_char_lower(nibble: int): char

Get hex character for nibble (0-15)

fn hex_char_upper(nibble: int): char

fn hex_char_value(c: char): int

Convert hex character to value (0-15), returns -1 for invalid

fn byte_to_hex(b: int): string

Converts a single byte (0-255) to a two-character hex string (lowercase)

fn byte_to_hex_upper(b: int): string

Converts a single byte (0-255) to a two-character hex string (uppercase)

fn hex_to_byte(hex_pair: string): result.Result[int, error.Error]

Converts a two-character hex string to a byte value. Ok(0-255) on a valid 2-character hex pair; Err(InvalidInput) on a too-short input or an invalid hex character.

fn hex_encode(data: string): string

Encodes a string to hexadecimal (lowercase) Each byte becomes two hex characters

fn hex_encode_upper(data: string): string

Encodes a string to hexadecimal (uppercase)

fn hex_decode(hex_str: string): result.Result[string, error.Error]

Decodes a hexadecimal string back to binary data. Ok(decoded) on success — empty input "" is valid hex and yields Ok(""). Err(InvalidInput) on odd length or an invalid hex character.

fn hex_is_valid(hex_str: string): bool

Checks if a string is valid hexadecimal

fn hex_encode_bytes(data: [int], len: int): string

Encodes a byte array to hexadecimal string (lowercase)

fn hex_decode_bytes(hex_str: string, out: [int], max: int): result.Result[int, error.Error]

Decodes a hexadecimal string into the caller-supplied byte array out (a deliberate low-level out-param API — not modernized to a fresh array). The param is named out rather than result to avoid colliding with the core.result module alias used for this function's own return type.

Ok(byte count) on success — empty input "" is valid hex and yields Ok(0). Err(InvalidInput) on odd length or an invalid hex character.


Generated by reefc doc