TheAlgorithms/Java · error · IllegalArgumentException

Base must be between 2 and 36

Error message

Base must be between 2 and 36

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/conversions/DecimalToAnyBase.java:33

    private static final char ZERO_CHAR = '0';
    private static final char A_CHAR = 'A';
    private static final int DIGIT_OFFSET = 10;

    private DecimalToAnyBase() {
    }

    /**
     * Converts a decimal number to a string representation in the specified base.
     * For example, converting the decimal number 10 to base 2 would return "1010".
     *
     * @param decimal the decimal number to convert
     * @param base    the base to convert to (must be between {@value #MIN_BASE} and {@value #MAX_BASE})
     * @return the string representation of the number in the specified base
     * @throws IllegalArgumentException if the base is out of the supported range
     */
    public static String convertToAnyBase(int decimal, int base) {
        if (base < MIN_BASE || base > MAX_BASE) {
            throw new IllegalArgumentException("Base must be between " + MIN_BASE + " and " + MAX_BASE);
        }

        if (decimal == 0) {
            return String.valueOf(ZERO_CHAR);
        }

        List<Character> digits = new ArrayList<>();
        while (decimal > 0) {
            digits.add(convertToChar(decimal % base));
            decimal /= base;
        }

        StringBuilder result = new StringBuilder(digits.size());
        for (int i = digits.size() - 1; i >= 0; i--) {
            result.append(digits.get(i));
        }

        return result.toString();

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate that base is between 2 and 36 inclusive before calling convertToAnyBase.
  2. Clamp or reject the user-supplied base at the input boundary with a clear error message.
  3. If only binary, octal, or hexadecimal are needed, use Integer.toString(n, radix) directly with a hardcoded radix.

Example fix

// before
String result = DecimalToAnyBase.convertToAnyBase(value, userInputBase);

// after
if (userInputBase < 2 || userInputBase > 36) {
    throw new IllegalArgumentException("Base must be between 2 and 36, got: " + userInputBase);
}
String result = DecimalToAnyBase.convertToAnyBase(value, userInputBase);
Defensive patterns

Strategy: validation

Validate before calling

if (base < 2 || base > 36) {
    throw new IllegalArgumentException("Base must be between 2 and 36, got: " + base);
}
String result = DecimalToAnyBase.convertToAnyBase(decimal, base);

Type guard

static boolean isValidBase(int base) {
    return base >= 2 && base <= 36;
}

Try / catch

try {
    String result = DecimalToAnyBase.convertToAnyBase(decimal, base);
} catch (IllegalArgumentException e) {
    // base was out of range; use a safe default or Integer.toString
    String result = Integer.toString(decimal, 10);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/ac226b78cdb1656b. Report an issue: GitHub.