{"record":{"id":"4be3c97f818a477c","repo":"TheAlgorithms/Java","slug":"invalid-hexadecimal-character","errorCode":null,"errorMessage":"Invalid hexadecimal character: {}","messagePattern":"Invalid hexadecimal character: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java","lineNumber":38,"sourceCode":"    }\n\n    /**\n     * Converts a hexadecimal string to its decimal integer equivalent.\n     * <p>The input string is case-insensitive, and must contain valid hexadecimal characters [0-9, A-F].</p>\n     *\n     * @param hex the hexadecimal string to convert\n     * @return the decimal integer representation of the input hexadecimal string\n     * @throws IllegalArgumentException if the input string contains invalid characters\n     */\n    public static int getHexaToDec(String hex) {\n        String digits = \"0123456789ABCDEF\";\n        hex = hex.toUpperCase();\n        int val = 0;\n\n        for (int i = 0; i < hex.length(); i++) {\n            int d = digits.indexOf(hex.charAt(i));\n            if (d == -1) {\n                throw new IllegalArgumentException(\"Invalid hexadecimal character: \" + hex.charAt(i));\n            }\n            val = 16 * val + d;\n        }\n\n        return val;\n    }\n}\n","sourceCodeStart":20,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java#L20-L46","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nint result = HexaDecimalToDecimal.getHexaToDec(hexInput); // hexInput may be '#FF00FF'\n\n// after\nString cleaned = hexInput.replaceAll(\"^0x|^#\", \"\").trim();\nif (!cleaned.matches(\"^[0-9A-Fa-f]+$\")) {\n    throw new IllegalArgumentException(\"Invalid hex string: \" + hexInput);\n}\nint result = HexaDecimalToDecimal.getHexaToDec(cleaned);","handlingStrategy":"validation","validationCode":"String cleaned = hex.replaceAll(\"^0x|^#\", \"\").trim();\nif (!cleaned.matches(\"^[0-9A-Fa-f]+$\")) {\n    throw new IllegalArgumentException(\"Invalid hex string: \" + hex);\n}\nint result = HexaDecimalToDecimal.getHexaToDec(cleaned);","typeGuard":"static boolean isValidHex(String s) {\n    return s != null && !s.isEmpty() && s.matches(\"^[0-9A-Fa-f]+$\");\n}","tryCatchPattern":"try {\n    int result = HexaDecimalToDecimal.getHexaToDec(hex);\n} catch (IllegalArgumentException e) {\n    // invalid hex character; log and skip or use default\n    logger.warn(\"Invalid hex input: {}\", hex);\n}","preventionTips":["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."],"tags":["number-conversion","input-validation","hexadecimal"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}