const_hex

Function const_decode_to_array

Source
pub const fn const_decode_to_array<const N: usize>(
    input: &[u8],
) -> 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.

Prefer using decode_to_array instead when possible (at runtime), as it is likely to be faster.

§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

const _: () = {
    let bytes = const_hex::const_decode_to_array(b"6b697769");
    assert!(matches!(bytes.as_ref(), Ok(b"kiwi")));

    let bytes = const_hex::const_decode_to_array(b"0x6b697769");
    assert!(matches!(bytes.as_ref(), Ok(b"kiwi")));
};