hexToInt method

int? hexToInt()

Converts a hexadecimal string to an integer (int).

This method attempts to parse the string as a hexadecimal number. It performs several checks to ensure the input is a valid hexadecimal string and that the resulting value can be represented as an int.

Returns: An int representation of the hexadecimal string, or null if the string is empty, not a valid hexadecimal string, or represents a value too large for an int.

Prints a warning to the debug console if the input is invalid or too large.

Example:

'FF'.hexToInt(); // Returns 255
'1A3F'.hexToInt(); // Returns 6719
''.hexToInt(); // Returns null
'invalid-hex'.hexToInt(); // Returns null, prints a warning
'FFFFFFFFFFFFFFFF'.hexToInt(); // Returns null, prints a warning (too large)

Implementation

int? hexToInt() {
  if (isEmpty) {
    return null;
  }
  // Check if the hexadecimal string is valid using a regular expression.
  if (!RegExp(r'^[0-9a-fA-F]+$').hasMatch(this)) {
    debugPrint('WARNING: Invalid [hexValue]: `$this`');
    return null;
  }
  // Check if the hexadecimal string is too large to be represented as an int.
  if (length > 16 || (length == 16 && compareTo('7FFFFFFFFFFFFFFF') > 0)) {
    debugPrint('WARNING: [hexValue] too large to be represented as an int: `$this`');
    return null;
  }
  // Convert the hexadecimal string to an int and return it.
  return int.parse(this, radix: 16);
}