Module encoding/hex

encoding/hex
Stability: unstable — two names short of complete, and the missing one is visible in the code. `_hex_encode_impl` already takes its alphabet as a parameter and `_HEX_CHARS` is the only value ever passed, so Rust's `hex::encode_upper` is one export away; that is additive and safe. The name is the part that is not settled: `hex_encode` / `hex_encode_upper` repeats the module (D2's module-prefix stutter, listed STILL OPEN for the whole encoding group in `plans/STD_API_STABILIZATION.md` §4), and the decided direction is `hex.encode` on the imported module value. Adding a second stuttering name first would mean renaming two instead of one. `hex_decode` became `Result`-returning in #467 (D13) and has not been through a release; its errors are the shared `EncodingError`, whose own open question — one enum for three codecs, where Rust has `hex::FromHexError` — is the other thing holding this module unstable, since `hex_decode` can only ever produce two of that enum's five variants. — stable modules only change additively; this one may still change.

Hexadecimal encoding and decoding.

Converts between raw bytes (ArrayList(u8)) and hex strings.

Example

hex :: import("std/encoding/hex");

s := hex.encode(data);         // "deadbeef"
b := hex.decode(`deadbeef`).unwrap();

Encoding is lowercase only; decoding accepts either case, and mixed case within one string.

Stability

unstable — two names short of complete, and the missing one is visible in the code. _hex_encode_impl already takes its alphabet as a parameter and _HEX_CHARS is the only value ever passed, so Rust's hex::encode_upper is one export away; that is additive and safe. The name is the part that is not settled: hex_encode / hex_encode_upper repeats the module (D2's module-prefix stutter, listed STILL OPEN for the whole encoding group in plans/STD_API_STABILIZATION.md §4), and the decided direction is hex.encode on the imported module value. Adding a second stuttering name first would mean renaming two instead of one.

hex_decode became Result-returning in #467 (D13) and has not been through a release; its errors are the shared EncodingError, whose own open question — one enum for three codecs, where Rust has hex::FromHexError — is the other thing holding this module unstable, since hex_decode can only ever produce two of that enum's five variants.

Functions

encode function
fn(data : ArrayList(u8)) -> String

Encode bytes as a lowercase hexadecimal string.

Parameters

NameTypeNotes
dataArrayList(u8)

Returns: String

decode function
fn(s : String) -> Result(ArrayList(u8), EncodingError)

Decode a hexadecimal string to bytes (D13 — a pure transform returns a Result; decode_exn is the effect-carrying wrapper).

Parameters

NameTypeNotes
sString

Returns: Result(ArrayList(u8), EncodingError)

decode_exn function
fn(s : String, exn : Exception) -> ArrayList(u8)

decode as an effect: throws the EncodingError through exn instead of returning it. Kept for callers already inside an effect scope.

Parameters

NameTypeNotes
sString
exnException

Returns: ArrayList(u8)

hex_encode function
fn(data : ArrayList(u8)) -> String

DEPRECATED, removed in v0.2.32: call hex.encode.

Parameters

NameTypeNotes
dataArrayList(u8)

Returns: String

hex_decode function
fn(s : String) -> Result(ArrayList(u8), EncodingError)

DEPRECATED, removed in v0.2.32: call hex.decode.

Parameters

NameTypeNotes
sString

Returns: Result(ArrayList(u8), EncodingError)

hex_decode_exn function
fn(s : String, exn : Exception) -> ArrayList(u8)

DEPRECATED, removed in v0.2.32: call hex.decode_exn.

Parameters

NameTypeNotes
sString
exnException

Returns: ArrayList(u8)