fromHexString static method

List<int> fromHexString(
  1. String data, {
  2. bool paddingZero = false,
})

Converts a hexadecimal string data into a List of integers representing bytes.

The method removes the '0x' prefix, strips leading zeros, and decodes the resulting hexadecimal string into bytes. Optionally, it pads zero if the string length is odd and the paddingZero parameter is set to true.

Parameters:

  • data: The hexadecimal string to be converted.
  • paddingZero: Whether to pad a zero to the string if its length is odd (default is false).

Returns:

  • A List of integers representing bytes converted from the hexadecimal string.

Throws:

Implementation

static List<int> fromHexString(String data, {bool paddingZero = false}) {
  try {
    String hexString = StringUtils.strip0x(data);
    if (hexString.isEmpty) return [];
    if (paddingZero && hexString.length.isOdd) {
      hexString = "0$hexString";
    }
    return hex.hex.decode(hexString);
  } catch (e) {
    throw const ArgumentException("invalid hex bytes");
  }
}