- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JAVA program to Convert Hexadecimal to Binary
Binary Number − There are four types of number systems available. Binary number is one of them. The Binary number is basically represented with two digits i.e. one (1) and zero (0). The binary numbers are expressed as base-2 in the numeral system.
Hexadecimal Number − Hexadecimal number is also one of the number systems available. The Hexadecimal number is represented with 16 digits which is from 0 to 15(0, 1, 2, 3... 15). From 10 to 15 it is represented as A to F. The Hexadecimal numbers are expressed as base-16 in the numeral system.
Here we convert the hexadecimal numbers into binary numbers, where we get the binary numbers combination of four digits for each digit. After getting all those binary digits we concatenate all those digits. By this way we convert the Hexadecimal number into a binary number.
In this article we will see how to convert hexadecimal to binary in Java.
To show you some instances −
Instance-1
Input Hexadecimal number is 9AD. The Binary converted value of it = 100110101101
Instance-2
Input octal number is 458 The Binary converted value of it = 100110101101.
Instance-3
Input octal number is 6E7D. The Binary converted value of it = 0110111001111101.
Algorithm
Step-1 − Get the input number as string type either by static input or user input method.
Step-2 − We create an object of hashMap class and append all the keys and values to it. In keys we give the digits of Hexadecimal number from 0 to F and in values we give the respected binary values.
Step-3 − We initiate a loop and run it until the length of the given hexadecimal ends.
Step-4 − In each iteration, we find the appropriate binary values of and concatenate that value to another variable.
Step-5 − At the end we print the calculated binary value as output.
Multiple Approaches
We have provided the solution in different approaches.
By User Defined Method with Static Input Values.
By User Defined Method with User Input Values.
Let’s see the program along with its output one by one.
Approach-1: By Using User Defined Method with Static Input Value
In this approach, we declare a hexadecimal input number by a static input method and pass this number as a parameter in a user defined method, then inside the method by using the algorithm we can convert the hexadecimal number into binary number.
Example
import java.util.HashMap; public class Main { public static void main(String[] args){ String hexadecimalNumber = "12Fd89"; System.out.println("Given Hexadecimal number is ="+hexadecimalNumber.toUpperCase()); System.out.println("The binary conversion of "+hexadecimalNumber.toUpperCase() + " is "+hexadecimalToBinary(hexadecimalNumber)); } public static String hexadecimalToBinary(String hexadecimalNumber){ String binaryNumber = ""; hexadecimalNumber = hexadecimalNumber.toUpperCase(); HashMap<Character, String> hm = new HashMap<Character, String>(); hm.put('0', "0000"); hm.put('1', "0001"); hm.put('2', "0010"); hm.put('3', "0011"); hm.put('4', "0100"); hm.put('5', "0101"); hm.put('6', "0110"); hm.put('7', "0111"); hm.put('8', "1000"); hm.put('9', "1001"); hm.put('A', "1010"); hm.put('B', "1011"); hm.put('C', "1100"); hm.put('D', "1101"); hm.put('E', "1110"); hm.put('F', "1111"); for (int i = 0; i < hexadecimalNumber.length(); i++) { char c = hexadecimalNumber.charAt(i); if (hm.containsKey(c)) binaryNumber += hm.get(c); else { binaryNumber = "You have entered an invalid Hexadecimal Number."; return binaryNumber; } } return binaryNumber; } }
Output
Given Hexadecimal number is = 12FD89 The binary conversion of 12FD89 is 000100101111110110001001
Approach-2: By Using User Defined Method with User Input Value
In this approach, we take user input of a hexadecimal number and pass this number as aparameter in a user defined method, then inside the method by using the algorithm we can convert the hexadecimal number into a binary number.
Example
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.print("Enter a Hexadecimal Number = "); String hexadecimalNumber = sc.nextLine(); System.out.println("Given Hexadecimal number is = "+hexadecimalNumber.toUpperCase()); System.out.println("The binary conversion of "+ hexadecimalNumber.toUpperCase() + " is " +hexadecimalToBinary(hexadecimalNumber)); } public static String hexadecimalToBinary(String hexadecimalNumber){ String binaryNumber = ""; hexadecimalNumber = hexadecimalNumber.toUpperCase(); HashMap<Character, String> hm = new HashMap<Character, String>(); hm.put('0', "0000"); hm.put('1', "0001"); hm.put('2', "0010"); hm.put('3', "0011"); hm.put('4', "0100"); hm.put('5', "0101"); hm.put('6', "0110"); hm.put('7', "0111"); hm.put('8', "1000"); hm.put('9', "1001"); hm.put('A', "1010"); hm.put('B', "1011"); hm.put('C', "1100"); hm.put('D', "1101"); hm.put('E', "1110"); hm.put('F', "1111"); for (int i = 0; i < hexadecimalNumber.length(); i++) { char c = hexadecimalNumber.charAt(i); if (hm.containsKey(c)) binaryNumber += hm.get(c); else { binaryNumber = "You have entered an invalid Hexadecimal Number."; return binaryNumber; } } return binaryNumber; } }
Output
Enter a Hexadecimal Number = de453a Given Hexadecimal number is = DE453A The binary conversion of DE453A is 110111100100010100111010
In this article, we explored how to convert a hexadecimal number to a binary number in Java by using different approaches.