uint8BufferFromHex function

Uint8Buffer uint8BufferFromHex(
  1. String hex, {
  2. bool utf8EncodeOnHexFailure = false,
})

convert hex string to Uint8Buffer. Strips off 0x prefix if present.

Implementation

Uint8Buffer uint8BufferFromHex(String hex,
    {bool utf8EncodeOnHexFailure = false}) {
  if (hex.isEmpty) return _emptyUint8Buffer;
  try {
    final list =
        hex.startsWith('0x') ? HEX.decode(hex.substring(2)) : HEX.decode(hex);
    final result = Uint8Buffer();
    result.addAll(list);
    return result;
  } catch (e) {
    if (!utf8EncodeOnHexFailure) rethrow;
    final list = utf8.encode(hex);
    final result = Uint8Buffer();
    result.addAll(list);
    return result;
  }
}