Home Java Java-conversion Convert Hexadecimal into Binary and Long
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert Hexadecimal into Binary and Long

Advertisement
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.io.*;
import java.lang.*;

public class HexadecimalToBinaryAndLong{
  public static void main(String[] argsthrows 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
Advertisements

Liked it!  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.

Posted on: June 15, 2007

Ask Questions?    Discuss: Convert Hexadecimal into Binary and Long   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
anon
September 2, 2011
hm?

how can this work? converting the string hex to int is not gonna fly - > what if the hex number happens to contain an (A-F) hex val? how would int handle that?
nufan
October 12, 2011
Wrong

Wow, this is pretty wrong.. hexavalue 14 = 16^1*1 + 16^0*4 = 20 in dec.. but 20 in dec is NOT 1110 in bin.. It's 10100. Integer.toBinaryString(i) needs a decimal number, but you put in 14 meaning hex.
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

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
Error

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.
dfjk
August 23, 2012
this is long

Is it??
DMCA.com