Javatpoint Logo
Javatpoint Logo

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 Character Equivalent Binary Value
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

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 Digit 4-bit Binary Equivalent
A 1010
D 1101
E 1110
2 0010

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA