hex_string_to_bytes

Function hex_string_to_bytes 

Source
pub fn hex_string_to_bytes<const N: usize>(
    hex_str: &str,
    expected_len: usize,
) -> Result<[u8; N], &'static str>
Expand description

Convert a hex string to a byte array of specified length

§Parameters

  • hex_str: Hex string (with or without “0x” prefix)
  • expected_len: Expected length of the resulting byte array

§Returns

  • Ok([u8; N]) if conversion succeeds
  • Err(&str) with error message if conversion fails

§Examples

use miracle_api::utils::hex::hex_string_to_bytes;

// Convert to 8-byte array
let bytes: [u8; 8] = hex_string_to_bytes("0x1234567890abcdef", 8)?;
assert_eq!(bytes, [0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]);

// Convert to 32-byte array
let bytes: [u8; 32] = hex_string_to_bytes("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", 32)?;