fromHexString static method

ByteArray fromHexString(
  1. String hexString
)

Creates a ByteArray from a hexadecimal string.

hexString the hex string (e.g., "48656C6C6F" for "Hello")

Implementation

static ByteArray fromHexString(String hexString) {
  // Remove any whitespace or separators
  String cleaned = hexString.replaceAll(RegExp(r'[^0-9A-Fa-f]'), '');

  if (cleaned.length % 2 != 0) {
    throw InvalidArgumentException('Hex string must have even length');
  }

  List<int> bytes = [];
  for (int i = 0; i < cleaned.length; i += 2) {
    String hexByte = cleaned.substring(i, i + 2);
    bytes.add(int.parse(hexByte, radix: 16));
  }

  return ByteArray.fromList(bytes);
}