hexToBytes static method
Hexadecimal to bytes
Implementation
static Uint8List hexToBytes(String hex) {
// hex to bytes
final l = hex.length;
if (l > 1 && l % 2 != 0) {
// or has odd length like 3, 5.
throw Exception('hex invalid 1');
}
// create result array
final arr = Uint8List(l ~/ 2);
for (var i = 0; i < arr.length; i++) {
final j = i * 2;
// hexByte. slice is faster than substr
final h = hex.substring(j, j + 2);
// byte, created from string part
final b = int.parse(h, radix: 16);
if (b.isNaN || b < 0) {
// byte must be valid 0 <= byte < 256
throw Exception('hex invalid 2');
}
arr[i] = b;
}
return arr;
}