{"record":{"id":"edb1b6fb25218e0a","repo":"TheAlgorithms/Java","slug":"incorrect-binary-digit-edb1b6","errorCode":null,"errorMessage":"Incorrect binary digit: {}","messagePattern":"Incorrect binary digit: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/BinaryToHexadecimal.java","lineNumber":37,"sourceCode":"    }\n\n    /**\n     * Converts a binary number to a hexadecimal number.\n     *\n     * @param binary The binary number to convert.\n     * @return The hexadecimal representation of the binary number.\n     * @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.\n     */\n    public static String binToHex(int binary) {\n        Map<Integer, String> hexMap = initializeHexMap();\n        StringBuilder hex = new StringBuilder();\n\n        while (binary != 0) {\n            int decimalValue = 0;\n            for (int i = 0; i < BITS_IN_HEX_DIGIT; i++) {\n                int currentBit = binary % BASE_DECIMAL;\n                if (currentBit > 1) {\n                    throw new IllegalArgumentException(\"Incorrect binary digit: \" + currentBit);\n                }\n                binary /= BASE_DECIMAL;\n                decimalValue += (int) (currentBit * Math.pow(BASE_BINARY, i));\n            }\n            hex.insert(0, hexMap.get(decimalValue));\n        }\n\n        return !hex.isEmpty() ? hex.toString() : \"0\";\n    }\n\n    /**\n     * Initializes the hexadecimal map with decimal to hexadecimal mappings.\n     *\n     * @return The initialized map containing mappings from decimal numbers to hexadecimal digits.\n     */\n    private static Map<Integer, String> initializeHexMap() {\n        Map<Integer, String> hexMap = new HashMap<>();\n        for (int i = 0; i < BASE_DECIMAL; i++) {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/BinaryToHexadecimal.java#L19-L55","documentation":"Thrown by BinaryToHexadecimal.binToHex(int) when a decimal digit of the binary input exceeds 1. Like the long-based binary converters, this method treats the int as a sequence of decimal digits that must be 0/1. Each group of 4 bits (consumed from least significant) is converted to one hex digit.","triggerScenarios":"Passing 102, 123, or any int containing a decimal digit 2-9. Passing a number intended as a binary string but represented as an int, losing leading zeros. Passing a hex or decimal value by mistake.","commonSituations":"Confusing int literal 0b1010 (Java binary literal) with the decimal 1010. Leading zeros lost when stored as int. User typos in binary input.","solutions":["Validate with String.valueOf(binary).matches(\"[01]+\") before calling.","Use Integer.toString(Integer.parseInt(binaryString, 2), 16) for a string-based path that preserves leading zeros.","Use Java binary literals (0b prefix) and Integer.toHexString() for in-code binary-to-hex conversion."],"exampleFix":"// before\nString hex = BinaryToHexadecimal.binToHex(102); // '2' digit rejected\n\n// after\nString hex = Integer.toString(Integer.parseInt(\"1010\", 2), 16); // \"a\"","handlingStrategy":"validation","validationCode":"if (!String.valueOf(binary).matches(\"[01]+\")) {\n    throw new IllegalArgumentException(\"not binary digits: \" + binary);\n}\nString hex = BinaryToHexadecimal.binToHex(binary);","typeGuard":"static boolean isBinaryInt(int n) {\n    return String.valueOf(n).matches(\"[01]+\");\n}","tryCatchPattern":"try {\n    String hex = BinaryToHexadecimal.binToHex(n);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"Invalid binary input: \" + n, e);\n}","preventionTips":["Use a string-based path (Integer.toString(Integer.parseInt(s,2),16)) to preserve leading zeros.","Validate the int's decimal digits are 0/1 before calling.","Use Java 0b literals + Integer.toHexString for in-code conversion."],"tags":["conversions","binary","hexadecimal","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}