- 1Convert Decimal to Binary in Java
- 2Convert Binary to Decimal in Java
- 3Convert Decimal to Octal in Java
- 4Convert Octal to Decimal in Java
- 5Convert Decimal to Hexadecimal in Java
- 6Convert Hexadecimal to Decimal in Java
- 7Convert Binary to Hexadecimal in Java
- 8Convert Hexadecimal to Binary in Java
- 9Convert Octal to Binary in Java
- 10Convert Temperature from Celsius to Fahrenheit in Java
- 11Convert Kilometers to Miles, Meters, and Centimeters in Java
- 12Convert a String to Uppercase and Lowercase in Java
Convert Hexadecimal to Binary in Java
Topic
In this program, we will convert a hexadecimal number (base-16) into its binary representation (base-2) using Java. Hexadecimal numbers use digits 0-9 and letters A-F to represent values from 0 to 15. Binary numbers only use 0 and 1.
In Java, we can use built-in functions like Integer.parseInt()
to convert hexadecimal strings into integers, and then Integer.toBinaryString()
to get the binary format. We'll explore both manual and built-in ways.
Examples
Example 1:
Input: 1A
Output: 11010
Explanation: '1A' in hexadecimal is 1×16 + 10 = 26 in decimal, and 26 in binary is 11010.
Example 2:
Input: F
Output: 1111
Explanation: 'F' in hexadecimal is 15 in decimal, and 15 in binary is 1111.
Example 3:
Input: 0
Output: 0
Explanation: Zero remains zero across all number systems.
Interviewer Expectations
The interviewer wants to see if you can:
- Understand number system conversions (hex to decimal to binary)
- Use Java's built-in methods effectively
- Handle edge cases like lowercase input, invalid characters, or '0'
- Write clean code and explain your approach clearly
Approach
We will take a hexadecimal number as a string input and follow these steps:
- Convert the hexadecimal string to decimal using
Integer.parseInt(hex, 16)
- Convert the decimal value to binary using
Integer.toBinaryString(decimal)
- Print the result
Dry Run
Input: 1A
Integer.parseInt("1A", 16)
→ 26Integer.toBinaryString(26)
→ "11010"- Output: 11010
Java Program
public class HexToBinary {
public static void main(String[] args) {
String hexNumber = "1A";
int decimal = Integer.parseInt(hexNumber, 16);
String binary = Integer.toBinaryString(decimal);
System.out.println("Hexadecimal: " + hexNumber);
System.out.println("Binary: " + binary);
}
}
Hexadecimal: 1A
Binary: 11010
Possible Followup Questions with Answers
1. What if the input is in lowercase?
Java's Integer.parseInt()
can handle both uppercase and lowercase characters. However, you can normalize it using hex.toUpperCase()
before parsing.
2. How to handle invalid characters in the input?
You can use a try-catch
block to catch NumberFormatException
:
try {
int decimal = Integer.parseInt(hex, 16);
} catch (NumberFormatException e) {
System.out.println("Invalid hexadecimal input!");
}
3. Can I convert each hex digit manually to binary?
Yes, you can map each hex digit (0–F) to a 4-bit binary string using a Map<Character, String>
or switch-case logic. This is useful if built-ins are not allowed.
4. How to convert binary back to hexadecimal?
Convert binary to decimal using Integer.parseInt(binary, 2)
, then to hexadecimal using Integer.toHexString(decimal)
.
String binary = "11010";
int decimal = Integer.parseInt(binary, 2);
String hex = Integer.toHexString(decimal).toUpperCase();
Comments
Loading comments...