const_hex

Function decode_to_array

source
pub fn decode_to_array<T: AsRef<[u8]>, const N: usize>(
    input: T,
) -> Result<[u8; N], FromHexError>
Expand description

Decode a hex string into a fixed-length byte-array.

Both, upper and lower case characters are valid in the input string and can even be mixed (e.g. f9b4ca, F9B4CA and f9B4Ca are all valid strings).

Strips the 0x prefix if present.

§Errors

This function returns an error if the input is not an even number of characters long or contains invalid hex characters, or if the input is not exactly N / 2 bytes long.

§Example

let bytes = const_hex::decode_to_array(b"6b697769").unwrap();
assert_eq!(&bytes, b"kiwi");

let bytes = const_hex::decode_to_array(b"0x6b697769").unwrap();
assert_eq!(&bytes, b"kiwi");