Share on Google+Share on Google+

Convert Hexadecimal into Binary and Long

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

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.lang.*;

public class HexadecimalToBinaryAndLong{
  public static void main(String[] argsthrows IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(;
  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>java HexadecimalToBinaryAndLong
Enter the hexa value!
This is Binary: 1110
This is long:=20


Posted on: June 15, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Convert Hexadecimal into Binary and Long   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
November 6, 2011

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

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

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

Hexadecimal 14 = Binary 10100
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

How can you parseInt a String having non digit characters. It is giving error.
Pancho Delgado
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.
August 23, 2012
this is long

Is it??