Java Hexadecimal to Binary Conversion

In programming, converting one type to/ from a type is a crucial task. Sometimes we require, conversion from one type to another type. In the Java conversion section, we have discoed various types of conversions. In this section, we can discuss how to convert hexadecimal into binary.

Hexadecimal Number

As the name suggests Hexadecimal number comprises 16 entities with 10 digits (0-9) that represent the first 10 hexadecimal numbers and the rest six entities represented by A to F (represented by the numbers 10 to 15). Note that the lowest number in the hexadecimal system is 0 and the highest is 15 represented by F. Its base is 16.

Java Hexadecimal to Binary Conversion

Hexadecimal to Binary Conversion

In order to convert any hexadecimal number to its binary equivalent, we must follow the steps given below:

  1. First, convert each hex digit to its 4-bit binary equivalent
  2. Combine all the 4-bit binary equivalent

The following table describes the hexadecimal number and corresponding binary equivalent.

Hexadecimal CharacterEquivalent Binary Value
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
A1010
B1011
C1100
D1101
E1110
F1111

Let's understand it through an example.

Example

Suppose, we have to convert AFB2 into binary equivalent.

Let's take a look at the table given below, which shows us the conversion of the above hexadecimal number to its binary equivalent.

Hex Digit4-bit Binary Equivalent
A1010
D1101
E1110
20010

From the above figure, we have four 4-bit binary equivalents, the first one is 1010, the second one is 1111, the third one is 1011, and the fourth one is 0010. On combining all four 4-bit binary numbers, we get 101010111100010. Therefore, (AFB2)16 = (101010111100010)2.

Java Program to Convert Binary to Hexadecimal

HexadecimalToBinary1.java

Output:

Enter the number: ab6da
Binary Value is: 10101011011011011010

Let's see another approach.

Using Switch Case

HexadecimalToBinary2.java

Output:

Enter the Hexadecimal Number: 2da
Equivalent Binary Value = 001011011010

Let's see another approach.

In this approach, first, we have initialized a constant LONG_BITS as 8. It defines the 8-bit long binary number. After that, we have defined a method conversion(). In this method, we have invoked Integer.parseInt() method.

Syntax:

The method accepts a string that contains the integer representation to be parsed and a radix (to be used while parsing s). It returns the integer represented by the string argument in the specified radix. It throws NumberFormatException If the string does not contain a parsable int.

Another method that we have invoked is Integer toBinaryString() method

Syntax:

The method accepts an integer to be converted to a string. It returns a string representation of the integer argument as an unsigned integer in base 2.

HexadecimalToBinary3.java

Output:

11 = 00010001
AD = 10101101
F4 = 11110100
BA = 10111010
AA = 10101010
AA = 10101010
13 = 00010011
01 = 00000001
02 = 00000010
03 = 00000011





Latest Courses