fromHexString static method

BuBytes fromHexString(
  1. String hexStr, {
  2. bool paddingZero = false,
})

Decodes a hex string (with an optional 0x prefix) into bytes.

paddingZero left-pads a zero when the string has odd length. Throws BuError on an invalid hex string.

Implementation

static BuBytes fromHexString(String hexStr, {bool paddingZero = false}) {
  try {
    String hexString = StringUtils.strip0x(hexStr);
    if (hexString.isEmpty) return BuBytes.empty();
    if (paddingZero && hexString.length.isOdd) {
      hexString = "0$hexString";
    }
    return hex.hex.decode(hexString);
  } catch (e) {
    throw CoreErrors.invalidOperationArguments(
      "fromHexString",
      name: "hexStr",
      reason: "invalid hex string.",
    );
  }
}