Skip to main content

hex_decode_array

Function hex_decode_array 

Source
pub fn hex_decode_array<const N: usize>(src: &[u8]) -> Result<[u8; N], Error>
Expand description

Decodes exactly N bytes into an array without allocation, accepting either case.

The input grammar is the same as hex_decode, but the decoded length must equal N: both shorter and longer inputs are rejected. The result owns its bytes independently of the input. Only N == 0 accepts empty input.

Use hex_decode_array_with_case for a strict letter-case policy, or hex_decode to write into a slice with spare capacity.

§Errors

Checks Error::OddLength first, Error::LengthMismatch second, and Error::InvalidChar last. The mismatch’s expected and actual fields both count decoded bytes; character positions count source bytes. Prefixes, whitespace and separators are rejected as invalid characters.

§Examples

use faster_hex::hex_decode_array;
let bytes = hex_decode_array::<4>(b"0001aBff")?;
assert_eq!(bytes, [0, 1, 0xab, 0xff]);
assert!(hex_decode_array::<4>(b"0001").is_err());
assert_eq!(hex_decode_array::<0>(b"")?, []);