hexStringToBytes static method

Uint8List hexStringToBytes(
  1. String hex
)

Converts a hex string to bytes.

Spaces in the hex string are ignored.

Implementation

static Uint8List hexStringToBytes(String hex) {
  // Delete blank space
  hex = hex.splitMapJoin(
    " ",
    onMatch: (Match match) {
      return "";
    },
  );
  if (hex.length % 2 != 0) {
    throw "Hex string length must be even integer, got ${hex.length}";
  }
  var result = <int>[];
  for (int i = 0; i < hex.length; i += 2) {
    result.add(int.parse(hex.substring(i, i + 2), radix: 16));
  }
  return Uint8List.fromList(result);
}