TheAlgorithms/Java · error · IllegalArgumentException
Bases must be between 2 and 10.
Error message
Bases must be between 2 and 10.
What it means
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.
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.
Example fix
// before int hex = AnytoAny.convertBase(255, 10, 16); // 16 > 10 -> throws // after String hex = Integer.toString(255, 16); // "ff", supports radix up to 36
Defensive patterns
Strategy: validation
Validate before calling
if (sourceBase < 2 || sourceBase > 10 || destBase < 2 || destBase > 10) {
throw new UnsupportedOperationException("bases outside [2,10] not supported; use Integer.parseInt with radix");
}
int result = AnytoAny.convertBase(sourceNumber, sourceBase, destBase); Type guard
static boolean basesSupportedByAnyToAny(int src, int dst) {
return src >= 2 && src <= 10 && dst >= 2 && dst <= 10;
} Try / catch
try {
int out = AnytoAny.convertBase(n, src, dst);
} catch (IllegalArgumentException e) {
// fall back to radix-capable converter
out = Integer.parseInt(String.valueOf(n), src);
// then convert via Integer.toString(out, dst)
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- For input string
- invalid character
- A padding '=' must not be followed by a non-padding…
- Incorrect binary digit
- Incorrect binary digit
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/4412c35d75720172.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/com/thealgorithms/conversions/AnytoAny.java:24
* This class provides a method to convert a source number from a given base
* to a destination number in another base. Valid bases range from 2 to 10.
*/
public final class AnytoAny {
private AnytoAny() {
}
/**
* Converts a number from a source base to a destination base.
*
* @param sourceNumber The number in the source base (as an integer).
* @param sourceBase The base of the source number (between 2 and 10).
* @param destBase The base to which the number should be converted (between 2 and 10).
* @throws IllegalArgumentException if the bases are not between 2 and 10.
* @return The converted number in the destination base (as an integer).
*/
public static int convertBase(int sourceNumber, int sourceBase, int destBase) {
if (sourceBase < 2 || sourceBase > 10 || destBase < 2 || destBase > 10) {
throw new IllegalArgumentException("Bases must be between 2 and 10.");
}
int decimalValue = toDecimal(sourceNumber, sourceBase);
return fromDecimal(decimalValue, destBase);
}
/**
* Converts a number from a given base to its decimal representation (base 10).
*
* @param number The number in the original base.
* @param base The base of the given number.
* @return The decimal representation of the number.
*/
private static int toDecimal(int number, int base) {
int decimalValue = 0;
int multiplier = 1;
while (number != 0) {View on GitHub (pinned to fdfb9a395b)