# Convert Hexadecimal into Binary and Long

In this section, you will learn to convert the hexadecimal data into the binary and long format.

Tutorials

# Convert Hexadecimal into Binary and Long

In this section, you will learn to convert the hexadecimal data into the binary and long format. The java.lang package provides the functionality to convert a hexadecimal to binary and long type data.

Code Description:

This program helps you in converting the hexadecimal data into the binary and long. At run time this program asks for a  a hexadecimal data and it then converts it into an integer type data. The toBinaryString() method converts an integer data into binary data. And the parseLong() method converts the hexadecimal data into a long type data.

Here is the code of this program:

 ``` import java.io.*; import java.lang.*; public class HexadecimalToBinaryAndLong{   public static void main(String[] args) throws IOException{   BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));   System.out.println("Enter the hexa value!");   String hex = bf.readLine();   int i = Integer.parseInt(hex);   String by = Integer.toBinaryString(i);   System.out.println("This is Binary: " + by);   long num = Long.parseLong(hex,16);   System.out.println("This is long:=" + num);   } } ```

`Download this program:`

`Output of this program.`

 C:\vinod\convert>javac HexadecimalToBinaryAndLong.java C:\vinod\convert>java HexadecimalToBinaryAndLong Enter the hexa value! 14 This is Binary: 1110 This is long:=20

# Convert Hexadecimal into Binary and Long

Related Tutorials

Subject (*):

java_p
November 6, 2011
Thanx!!

thank u very much for this splendid piece of code!!
Alex
January 25, 2012
Convert

What about I enter a hexa value like: A, B, 9B ?
Alex
January 25, 2012
Converter

And one thing more, .toBinaryString() does not return first digits if this are "0". Integer.toBinaryString(3) return 11 not 0011.
Shan
February 19, 2012
Mistake in the output

Monesh Kumar
February 24, 2012
64 bit Hex to binary conversion

public class HexData { public static void main(String[] args) { String hexData = "0023456789ABCDEF"; long longData; String binData = null; int balZero = 0; System.out.println("Hex Data : " + hexData); longData = Long.parseLong(hexData, 16); System.out.println("Long Data : " + longData); binData = Long.toBinaryString(longData); balZero = (64 - (binData.length()-1)); for(int i=1;i<balZero;i++) { binData = "0" + binData; } System.out.println("Bin Data : " + binData); } }
Manjeet Harsh
March 29, 2012
Rectify mistake

The logic of conversion from Hexadecimal to Integer is wrong. 14 should have been converted into 10100 binary equivalent. And the binary 1110 is equivalent to E in Hexadecimal.
Vibhav Sinha
August 5, 2012
Error

How can you parseInt a String having non digit characters. It is giving error.
August 10, 2012
Hexidecimal to Binary Conversion Fix

int i = Integer.parseInt(hex); should be int i = Integer.parseInt(hex,16); to accomodate a-f Change that and it works great - Thank you so much for the example.
dfjk
August 23, 2012
this is long

Is it??