Function binascii::hex2bin[][src]

pub fn hex2bin<'a>(
    input: &[u8],
    output: &'a mut [u8]
) -> Result<&'a mut [u8], ConvertError>

Base16 Decoder - Converts a hexadecimal string to it's binary form.

Example

use binascii::hex2bin;

let mut my_output_buffer = [0u8; 200];

// If `hex2bin` succeedes, the result will be a `slice` of `my_output_buffer` containing the decoded data.
let res = hex2bin("48656C6C6F2C20576F726C6421".as_bytes(), &mut my_output_buffer);

assert_eq!(res.ok().unwrap(), "Hello, World!".as_bytes());

Failures

This function will fail with:

  • ConvertError::InvalidInputLength - If the input slice's length is an odd number.
  • ConvertError::InvalidOutputLength - If the output's length isn't at least half of input's length.
  • ConvertError::InvalidInput - If the input contains characters that are not valid hex digits.