hexToBytes static method

ByteArray hexToBytes(
  1. HexString hex
)

Convert a hexadecimal string to a byte array

Implementation

static ByteArray hexToBytes(HexString hex) {
  String cleanHex = hex.toLowerCase();
  if (cleanHex.startsWith('0x')) {
    cleanHex = cleanHex.substring(2);
  }
  if (cleanHex.length % 2 != 0) {
    cleanHex = '${0}$cleanHex';
  }
  final bytes = Uint8List(cleanHex.length ~/ 2);
  for (int i = 0; i < cleanHex.length; i += 2) {
    bytes[i ~/ 2] = int.parse(cleanHex.substring(i, i + 2), radix: 16);
  }
  return bytes;
}