isHex function

bool isHex(
  1. dynamic value, [
  2. int bitLength = -1,
  3. bool ignoreLength = false
])

Implementation

bool isHex(dynamic value, [int bitLength = -1, bool ignoreLength = false]) {
  final reg = RegExp(r"^0x[a-fA-F0-9]+$");
  final isValidHex = value == '0x' ||
      (isString(value) && reg.allMatches(value.toString()).isNotEmpty);
  if (isValidHex && bitLength != -1) {
    return (value as String).length == (2 + (bitLength / 4).ceil());
  }

  return isValidHex && (ignoreLength || ((value as String).length % 2 == 0));
}