const_hex

Function decode_to_slice

Source
pub fn decode_to_slice<T: AsRef<[u8]>>(
    input: T,
    output: &mut [u8],
) -> Result<(), FromHexError>
Expand description

Decode a hex string into a mutable bytes slice.

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 output slice is not exactly half the length of the input.

§Example

let mut bytes = [0u8; 4];
const_hex::decode_to_slice("6b697769", &mut bytes).unwrap();
assert_eq!(&bytes, b"kiwi");

const_hex::decode_to_slice("0x6b697769", &mut bytes).unwrap();
assert_eq!(&bytes, b"kiwi");