{"record":{"id":"4412c35d75720172","repo":"TheAlgorithms/Java","slug":"bases-must-be-between-2-and-10","errorCode":null,"errorMessage":"Bases must be between 2 and 10.","messagePattern":"Bases must be between 2 and 10\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/AnytoAny.java","lineNumber":24,"sourceCode":" * This class provides a method to convert a source number from a given base\n * to a destination number in another base. Valid bases range from 2 to 10.\n */\npublic final class AnytoAny {\n    private AnytoAny() {\n    }\n\n    /**\n     * Converts a number from a source base to a destination base.\n     *\n     * @param sourceNumber The number in the source base (as an integer).\n     * @param sourceBase The base of the source number (between 2 and 10).\n     * @param destBase The base to which the number should be converted (between 2 and 10).\n     * @throws IllegalArgumentException if the bases are not between 2 and 10.\n     * @return The converted number in the destination base (as an integer).\n     */\n    public static int convertBase(int sourceNumber, int sourceBase, int destBase) {\n        if (sourceBase < 2 || sourceBase > 10 || destBase < 2 || destBase > 10) {\n            throw new IllegalArgumentException(\"Bases must be between 2 and 10.\");\n        }\n\n        int decimalValue = toDecimal(sourceNumber, sourceBase);\n        return fromDecimal(decimalValue, destBase);\n    }\n\n    /**\n     * Converts a number from a given base to its decimal representation (base 10).\n     *\n     * @param number The number in the original base.\n     * @param base The base of the given number.\n     * @return The decimal representation of the number.\n     */\n    private static int toDecimal(int number, int base) {\n        int decimalValue = 0;\n        int multiplier = 1;\n\n        while (number != 0) {","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/AnytoAny.java#L6-L42","documentation":"AnytoAny.convertBase() restricts both sourceBase and destBase to the range [2, 10]. Any base outside this interval (including the common bases 11-36 used for hex and beyond) is rejected. This is an intentional scope limit because digit representation beyond '9' would require letters, which this implementation does not support.","triggerScenarios":"Calling convertBase(num, 16, 10) or convertBase(num, 10, 16) to convert to/from hexadecimal. Passing base 0, 1, or a negative base. Passing base values above 10 for any radix.","commonSituations":"Assuming the utility handles hexadecimal (base 16). Reading base from config without clamping to the supported range. Migrating from a converter that supports bases up to 36.","solutions":["For bases > 10 (hex, base32, base36), use AnyBaseToDecimal with radix up to 36, or Integer.parseInt/String.valueOf with radix.","Validate and clamp the base to [2,10] before calling, or reject unsupported bases earlier in your pipeline.","Switch to a converter that supports the extended range if your use case needs bases beyond 10."],"exampleFix":"// before\nint hex = AnytoAny.convertBase(255, 10, 16); // 16 > 10 -> throws\n\n// after\nString hex = Integer.toString(255, 16); // \"ff\", supports radix up to 36","handlingStrategy":"validation","validationCode":"if (sourceBase < 2 || sourceBase > 10 || destBase < 2 || destBase > 10) {\n    throw new UnsupportedOperationException(\"bases outside [2,10] not supported; use Integer.parseInt with radix\");\n}\nint result = AnytoAny.convertBase(sourceNumber, sourceBase, destBase);","typeGuard":"static boolean basesSupportedByAnyToAny(int src, int dst) {\n    return src >= 2 && src <= 10 && dst >= 2 && dst <= 10;\n}","tryCatchPattern":"try {\n    int out = AnytoAny.convertBase(n, src, dst);\n} catch (IllegalArgumentException e) {\n    // fall back to radix-capable converter\n    out = Integer.parseInt(String.valueOf(n), src);\n    // then convert via Integer.toString(out, dst)\n}","preventionTips":["Use Integer.parseInt/Integer.toString with radix for bases up to 36.","Document the [2,10] limit at the API boundary of your own code.","Validate base range before calling convertBase."],"tags":["conversions","base-conversion","radix-range","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}