TheAlgorithms/Java · error · IllegalArgumentException
Invalid hexadecimal character: {}
Error message
Invalid hexadecimal character: {} What it means
Thrown by HexaDecimalToDecimal.getHexaToDec when the input string contains a character not found in the lookup string '0123456789ABCDEF'. The method uppercases the input first, so lowercase a–f are accepted, but any other character (spaces, punctuation, G–Z, special symbols) triggers this error.
Source
Thrown at src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java:38
}
/**
* Converts a hexadecimal string to its decimal integer equivalent.
* <p>The input string is case-insensitive, and must contain valid hexadecimal characters [0-9, A-F].</p>
*
* @param hex the hexadecimal string to convert
* @return the decimal integer representation of the input hexadecimal string
* @throws IllegalArgumentException if the input string contains invalid characters
*/
public static int getHexaToDec(String hex) {
String digits = "0123456789ABCDEF";
hex = hex.toUpperCase();
int val = 0;
for (int i = 0; i < hex.length(); i++) {
int d = digits.indexOf(hex.charAt(i));
if (d == -1) {
throw new IllegalArgumentException("Invalid hexadecimal character: " + hex.charAt(i));
}
val = 16 * val + d;
}
return val;
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Sanitize the input: strip whitespace and remove common hex prefixes like '0x' or '#' before calling getHexaToDec.
- Validate each character against [0-9A-Fa-f] using a regex like ^[0-9A-Fa-f]+$ before conversion.
- If the input may be empty, check for emptiness first and decide on a default or error.
Example fix
// before
int result = HexaDecimalToDecimal.getHexaToDec(hexInput); // hexInput may be '#FF00FF'
// after
String cleaned = hexInput.replaceAll("^0x|^#", "").trim();
if (!cleaned.matches("^[0-9A-Fa-f]+$")) {
throw new IllegalArgumentException("Invalid hex string: " + hexInput);
}
int result = HexaDecimalToDecimal.getHexaToDec(cleaned); Defensive patterns
Strategy: validation
Validate before calling
String cleaned = hex.replaceAll("^0x|^#", "").trim();
if (!cleaned.matches("^[0-9A-Fa-f]+$")) {
throw new IllegalArgumentException("Invalid hex string: " + hex);
}
int result = HexaDecimalToDecimal.getHexaToDec(cleaned); Type guard
static boolean isValidHex(String s) {
return s != null && !s.isEmpty() && s.matches("^[0-9A-Fa-f]+$");
} Try / catch
try {
int result = HexaDecimalToDecimal.getHexaToDec(hex);
} catch (IllegalArgumentException e) {
// invalid hex character; log and skip or use default
logger.warn("Invalid hex input: {}", hex);
} Prevention
- Strip common hex prefixes ('0x', '#') and whitespace before conversion.
- Use a regex validation at the input boundary to reject non-hex strings early.
- If the input may be empty, check for emptiness first since the method does not reject it.
When it happens
Trigger: Passing a string containing characters outside 0–9 and A–F (case-insensitive), such as 'GG', '12 34', '#FF00FF', or '0x1A'. Passing an empty string produces no character iteration (no error), but whitespace or sign characters like '+' or '-' will trigger it.
Common situations: Reading hex strings from a file or database with formatting characters (e.g., '0x' prefix, spaces, dashes). Accepting user input without stripping whitespace or prefix notation. Copying color hex codes that include a '#' prefix.
Related errors
- Base must be between 2 and 36
- Decimal number cannot be negative.
- Input cannot be null or empty
- Incorrect input: Expecting an octal number (digits 0-7)
- Input cannot be null or empty
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/4be3c97f818a477c.
Report an issue: GitHub.