pub const fn from_hex<const N: usize>(hex_str: &str) -> [u8; N] {
	let hex_bytes = hex_str.as_bytes();

	if hex_bytes.len() != N * 2 {
		panic!("input hex string size is incorrect");
	}

	let mut res = [0u8; N];

	let mut i = 0;
	while i < N {
		let left_byte = hex_bytes[2 * i];
		let right_byte = hex_bytes[2 * i + 1];

		let left_val = match left_byte {
			d if ('0' as u8 <= d && d <= '9' as u8) => d - '0' as u8,
			u if ('A' as u8 <= u && u <= 'F' as u8) => u - 'A' as u8 + 10u8,
			l if ('a' as u8 <= l && l <= 'f' as u8) => l - 'a' as u8 + 10u8,
			_ => panic!("found invalid hex character")
		};

		let right_val = match right_byte {
			d if ('0' as u8 <= d && d <= '9' as u8) => d - '0' as u8,
			u if ('A' as u8 <= u && u <= 'F' as u8) => u - 'A' as u8 + 10u8,
			l if ('a' as u8 <= l && l <= 'f' as u8) => l - 'a' as u8 + 10u8,
			_ => panic!("found invalid hex character")
		};

		res[i] = left_val << 4 | right_val;

		i += 1;
	}

	res
}
// Converts hex string to byte array at compile time
Converts hex string to byte array at compile time
Converts hex string to byte array at compile time
pub const fn from_hex<const N: usize>(hex_str: &str) -> [u8; N] {
	let hex_bytes = hex_str.as_bytes();
	if hex_bytes.len() != N * 2 {
		panic!("input hex string size is incorrect");
	}
	let mut res = [0u8; N];
	let mut i = 0;
	while i < N {
		let left_byte = hex_bytes[2 * i];
		let right_byte = hex_bytes[2 * i + 1];
		let left_val = match left_byte {
			d if ('0' as u8 <= d && d <= '9' as u8) => d - '0' as u8,
			u if ('A' as u8 <= u && u <= 'F' as u8) => u - 'A' as u8 + 10u8,
			l if ('a' as u8 <= l && l <= 'f' as u8) => l - 'a' as u8 + 10u8,
			_ => panic!("found invalid hex character")
		};
		let right_val = match right_byte {
			d if ('0' as u8 <= d && d <= '9' as u8) => d - '0' as u8,
			u if ('A' as u8 <= u && u <= 'F' as u8) => u - 'A' as u8 + 10u8,
			l if ('a' as u8 <= l && l <= 'f' as u8) => l - 'a' as u8 + 10u8,
			_ => panic!("found invalid hex character")
		};
		res[i] = left_val << 4 | right_val;
		i += 1;
	}
	res
}
pub const fn from_hex < const N : usize > ( hex_str : & str ) -> [ u8 ; N ] { let hex_bytes = hex_str . as_bytes ( ) ; if hex_bytes . len ( ) != N * 2 { panic ! ( " " ) ; } let mut res = [ 0u8 ; N ] ; let mut i = 0 ; while i < N { let left_byte = hex_bytes [ 2 * i ] ; let right_byte = hex_bytes [ 2 * i + 1 ] ; let left_val = match left_byte { d if ( '0' as u8 <= d && d <= '9' as u8 ) => d - '0' as u8 , u if ( 'A' as u8 <= u && u <= 'F' as u8 ) => u - 'A' as u8 + 10u8 , l if ( 'a' as u8 <= l && l <= 'f' as u8 ) => l - 'a' as u8 + 10u8 , _ => panic ! ( " " ) } ; let right_val = match right_byte { d if ( '0' as u8 <= d && d <= '9' as u8 ) => d - '0' as u8 , u if ( 'A' as u8 <= u && u <= 'F' as u8 ) => u - 'A' as u8 + 10u8 , l if ( 'a' as u8 <= l && l <= 'f' as u8 ) => l - 'a' as u8 + 10u8 , _ => panic ! ( " " ) } ; res [ i ] = left_val << 4 | right_val ; i += 1 ; } res }
Converts hex string to byte array at compile time
Converts hex string to byte array at compile time
hex_str
&str