amebazii::types

Function key_from_hex

Source
pub fn key_from_hex<const N: usize>(hexstr: &str) -> DataType<N>
Expand description

Converts a hexadecimal string into a DataType array.

This function takes a hexadecimal string (hexstr), decodes it into bytes, and then attempts to convert the bytes into a DataType of a specific size.

The size of the resulting DataType is determined by the constant N. This function will panic if the length of the decoded byte array does not match the expected size N.

§Type Parameters:

  • N: The size of the DataType array. This is a constant array length that the decoded hexadecimal string must match. It is passed at compile-time to ensure type safety.

§Arguments:

  • hexstr: A string containing the hexadecimal representation of the key. The string must contain an even number of characters (each representing a byte).

§Returns:

  • Some(DataType<N>): A DataType array of size N, constructed from the decoded bytes. Returns None if the hexadecimal string is empty or the decoded bytes do not match the expected length.

§Panics:

  • This function will panic if the length of the decoded byte array is not equal to N.

§Example:

let hexstr = "a1b2c3d4e5f67890";
let key = key_from_hex::<8>(hexstr);
assert_eq!(key, Some([0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf6, 0x78, 0x90]));