hexToBytes static method

Uint8List hexToBytes(
  1. String hex
)

Converts a hex string to a byte array.

Implementation

static Uint8List hexToBytes(String hex) {
  // Normalize the hex string
  final normalizedHex = hex.replaceAll(' ', '').toLowerCase();

  // Check for valid hex string
  if (normalizedHex.length % 2 != 0) {
    throw ArgumentError('Hex string must have an even number of characters');
  }

  final bytes = Uint8List(normalizedHex.length ~/ 2);

  for (var i = 0; i < bytes.length; i++) {
    final byteHex = normalizedHex.substring(i * 2, i * 2 + 2);
    bytes[i] = int.parse(byteHex, radix: 16);
  }

  return bytes;
}