const_hex

Function decode

Source
pub fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, FromHexError>
Available on crate feature alloc only.
Expand description

Decodes a hex string into raw bytes.

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.

§Example

assert_eq!(
    const_hex::decode("48656c6c6f20776f726c6421"),
    Ok("Hello world!".to_owned().into_bytes())
);
assert_eq!(
    const_hex::decode("0x48656c6c6f20776f726c6421"),
    Ok("Hello world!".to_owned().into_bytes())
);

assert_eq!(const_hex::decode("123"), Err(const_hex::FromHexError::OddLength));
assert!(const_hex::decode("foo").is_err());