TheAlgorithms/Java · error · IllegalArgumentException
Incorrect binary digit: {}
Error message
Incorrect binary digit: {} What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/conversions/BinaryToHexadecimal.java:37
}
/**
* Converts a binary number to a hexadecimal number.
*
* @param binary The binary number to convert.
* @return The hexadecimal representation of the binary number.
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
*/
public static String binToHex(int binary) {
Map<Integer, String> hexMap = initializeHexMap();
StringBuilder hex = new StringBuilder();
while (binary != 0) {
int decimalValue = 0;
for (int i = 0; i < BITS_IN_HEX_DIGIT; i++) {
int currentBit = binary % BASE_DECIMAL;
if (currentBit > 1) {
throw new IllegalArgumentException("Incorrect binary digit: " + currentBit);
}
binary /= BASE_DECIMAL;
decimalValue += (int) (currentBit * Math.pow(BASE_BINARY, i));
}
hex.insert(0, hexMap.get(decimalValue));
}
return !hex.isEmpty() ? hex.toString() : "0";
}
/**
* Initializes the hexadecimal map with decimal to hexadecimal mappings.
*
* @return The initialized map containing mappings from decimal numbers to hexadecimal digits.
*/
private static Map<Integer, String> initializeHexMap() {
Map<Integer, String> hexMap = new HashMap<>();
for (int i = 0; i < BASE_DECIMAL; i++) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
String hex = BinaryToHexadecimal.binToHex(102); // '2' digit rejected
// after
String hex = Integer.toString(Integer.parseInt("1010", 2), 16); // "a" Defensive patterns
Strategy: validation
Validate before calling
if (!String.valueOf(binary).matches("[01]+")) {
throw new IllegalArgumentException("not binary digits: " + binary);
}
String hex = BinaryToHexadecimal.binToHex(binary); Type guard
static boolean isBinaryInt(int n) {
return String.valueOf(n).matches("[01]+");
} Try / catch
try {
String hex = BinaryToHexadecimal.binToHex(n);
} catch (IllegalArgumentException e) {
throw new DomainException("Invalid binary input: " + n, e);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Confusing int literal 0b1010 (Java binary literal) with the decimal 1010. Leading zeros lost when stored as int. User typos in binary input.
Related errors
- Incorrect binary digit: {}
- Input is not a valid binary number.
- Slope and intercept must be valid numbers.
- For input string: {}
- Input cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/edb1b6fb25218e0a.
Report an issue: GitHub.