Skip to main content

decode

Function decode 

Source
pub fn decode(hex: &str) -> Result<Vec<u8>, HexError>
Expand description

Decodes a hex string to bytes.

Accepts lowercase, uppercase, and mixed-case input. An optional 0x or 0X prefix is stripped before decoding.

§Errors

Returns HexError::OddLength if the number of hex digits is odd, or HexError::InvalidChar if any character is not a valid hex digit.

§Examples

use rune_hex::decode;

assert_eq!(decode("deadbeef").unwrap(), b"\xde\xad\xbe\xef");
assert_eq!(decode("DEADBEEF").unwrap(), b"\xde\xad\xbe\xef");
assert_eq!(decode("0xDeAdBeEf").unwrap(), b"\xde\xad\xbe\xef");
assert_eq!(decode("").unwrap(), b"");