{"record":{"id":"ac226b78cdb1656b","repo":"TheAlgorithms/Java","slug":"base-must-be-between-2-and-36","errorCode":null,"errorMessage":"Base must be between 2 and 36","messagePattern":"Base must be between 2 and 36","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/DecimalToAnyBase.java","lineNumber":33,"sourceCode":"    private static final char ZERO_CHAR = '0';\n    private static final char A_CHAR = 'A';\n    private static final int DIGIT_OFFSET = 10;\n\n    private DecimalToAnyBase() {\n    }\n\n    /**\n     * Converts a decimal number to a string representation in the specified base.\n     * For example, converting the decimal number 10 to base 2 would return \"1010\".\n     *\n     * @param decimal the decimal number to convert\n     * @param base    the base to convert to (must be between {@value #MIN_BASE} and {@value #MAX_BASE})\n     * @return the string representation of the number in the specified base\n     * @throws IllegalArgumentException if the base is out of the supported range\n     */\n    public static String convertToAnyBase(int decimal, int base) {\n        if (base < MIN_BASE || base > MAX_BASE) {\n            throw new IllegalArgumentException(\"Base must be between \" + MIN_BASE + \" and \" + MAX_BASE);\n        }\n\n        if (decimal == 0) {\n            return String.valueOf(ZERO_CHAR);\n        }\n\n        List<Character> digits = new ArrayList<>();\n        while (decimal > 0) {\n            digits.add(convertToChar(decimal % base));\n            decimal /= base;\n        }\n\n        StringBuilder result = new StringBuilder(digits.size());\n        for (int i = digits.size() - 1; i >= 0; i--) {\n            result.append(digits.get(i));\n        }\n\n        return result.toString();","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/DecimalToAnyBase.java#L15-L51","documentation":"Thrown by DecimalToAnyBase.convertToAnyBase when the base argument is below MIN_BASE (2) or above MAX_BASE (36). The range 2–36 is the maximum expressible with the ten digits 0–9 plus twenty-six letters A–Z, matching Java's Integer.toString radix convention.","triggerScenarios":"Calling convertToAnyBase(decimal, 1), convertToAnyBase(decimal, 0), convertToAnyBase(decimal, 37), or any base outside [2, 36]. Passing a negative base. Passing a base derived from a user-controlled or config-supplied value without clamping.","commonSituations":"Configuring a custom numeral system from a properties file where the base key is mistyped or defaults to 0. Accepting a user-supplied base string and parsing it without range validation. Copying code that used Integer.toString(n, radix) and supplying an out-of-range radix.","solutions":["Validate that base is between 2 and 36 inclusive before calling convertToAnyBase.","Clamp or reject the user-supplied base at the input boundary with a clear error message.","If only binary, octal, or hexadecimal are needed, use Integer.toString(n, radix) directly with a hardcoded radix."],"exampleFix":"// before\nString result = DecimalToAnyBase.convertToAnyBase(value, userInputBase);\n\n// after\nif (userInputBase < 2 || userInputBase > 36) {\n    throw new IllegalArgumentException(\"Base must be between 2 and 36, got: \" + userInputBase);\n}\nString result = DecimalToAnyBase.convertToAnyBase(value, userInputBase);","handlingStrategy":"validation","validationCode":"if (base < 2 || base > 36) {\n    throw new IllegalArgumentException(\"Base must be between 2 and 36, got: \" + base);\n}\nString result = DecimalToAnyBase.convertToAnyBase(decimal, base);","typeGuard":"static boolean isValidBase(int base) {\n    return base >= 2 && base <= 36;\n}","tryCatchPattern":"try {\n    String result = DecimalToAnyBase.convertToAnyBase(decimal, base);\n} catch (IllegalArgumentException e) {\n    // base was out of range; use a safe default or Integer.toString\n    String result = Integer.toString(decimal, 10);\n}","preventionTips":["Define base constants (BINARY=2, OCTAL=8, DECIMAL=10, HEX=16) and use them instead of magic numbers.","If the base comes from config, validate it at startup with a fail-fast check.","Consider Integer.toString(n, radix) for standard bases — it has the same range constraint but is built-in."],"tags":["number-conversion","input-validation","range-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}