pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>>Expand description
Converts a hexadecimal string to a vector of bytes.
Parses a string of hexadecimal digits (case-insensitive) and converts them to their binary representation. Each pair of hex digits becomes one byte in the output.
§Parameters
hex- A string slice containing hexadecimal digits (0-9, a-f, A-F)
§Returns
Ok(Vec<u8>)- A vector containing the decoded bytesErr(Error::StringConversionError)- If the string has odd length or contains invalid hex digits
§Memory Allocation
This function allocates a Vec on the heap. For no-alloc environments,
use hex_to_bytes_into_slice instead.
§Examples
ⓘ
use osal_rs::utils::hex_to_bytes;
// Lowercase hex
let bytes = hex_to_bytes("0123abff").unwrap();
assert_eq!(bytes, vec![0x01, 0x23, 0xAB, 0xFF]);
// Uppercase hex
let bytes2 = hex_to_bytes("ABCD").unwrap();
assert_eq!(bytes2, vec![0xAB, 0xCD]);
// Odd length - error
assert!(hex_to_bytes("ABC").is_err());
// Invalid character - error
assert!(hex_to_bytes("0G").is_err());