Expand description
Fast hex encoding and decoding with SIMD acceleration, constant-time
operations, and const fn support.
Encoding supports lowercase (0-9a-f) and uppercase (0-9A-F)
alphabets via Alphabet. Decoding is case-insensitive.
§Feature flags
| Flag | Description |
|---|---|
std | std::error::Error trait impls (enabled by default) |
alloc | String/Vec-returning convenience APIs |
serde | Serde serialize/deserialize helpers |
§Performance
The encode_into and decode_into functions
automatically dispatch to SIMD-accelerated paths (AVX2 on x86/x86_64,
NEON on aarch64). When a constant-time guarantee is required, use
encode_into_constant_time or decode_into_constant_time.
§const fn support
encode_array and decode_array are const fn, enabling hex
encoding and decoding at compile time.
§Examples
let encoded = hex::encode(b"hello");
assert_eq!(encoded, "68656c6c6f");
let decoded = hex::decode(b"68656c6c6f").unwrap();
assert_eq!(decoded, b"hello");
let upper = hex::encode_with_alphabet(b"hello", hex::Alphabet::Upper);
assert_eq!(upper, "68656C6C6F");Enums§
- Alphabet
- The hex character set used for encoding.
- Decode
Error - Errors that can occur during hex decoding.
- Encode
Error - Errors that can occur during hex encoding.
Functions§
- decode
alloc - Decode Decodes a hex string into bytes.
- decode_
array - Decodes a hex string into a fixed-size array at compile time.
- decode_
into - Decodes a hex string into an existing buffer.
- decode_
into_ constant_ time - Constant-time hex decoding. Processes all input data without secret-dependent branches or memory accesses, making it suitable for cryptographic applications.
- encode
alloc - Encodes bytes to a lowercase hex string.
- encode_
array - Encodes
datainto a fixed-size array at compile time. - encode_
into - Encodes bytes into an existing buffer.
- encode_
into_ constant_ time - Constant-time hex encoding. Processes all input data without secret-dependent branches or memory accesses, making it suitable for cryptographic applications.
- encode_
into_ string alloc - Appends the hex-encoded representation of
datato aString. - encode_
with_ alphabet alloc - Encodes bytes to a hex string using the given
Alphabet.