Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Java Java-conversion Convert Binary to Decimal

Related Tutorials


Share on Google+Share on Google+

Convert Binary to Decimal

In this section, you will learn how to convert a binary number into a decimal number.

Convert Binary to Decimal


In this section, you will learn how to convert  binary number  into a decimal number. The java.lang package provides the facility to convert the integer data into the binary to decimal. 

Code Description:

In this program, you will learn the use of parseLong() method.  Define a class "BinaryToDecimal" using the  parseLong() method. This method is used to parse the string argument as a signed decimal long. This method has been used for converting a long to a string and a String to a long.

Here is the code of this program:

import java.lang.*;

public class BinaryToDecimal{
  public static void main(String[] argsthrows IOException{
  BufferedReader bf= new BufferedReader(new InputStreamReader(;
  System.out.print("Enter the Binary value: ");
  String str = bf.readLine();
  long num = Long.parseLong(str);
  long rem;
  while(num > 0){
  rem = num % 10;
  num = num / 10;
  if(rem != && rem != 1){
  System.out.println("This is not a binary number.");
  System.out.println("Please try once again.");
  int i= Integer.parseInt(str,2);
  System.out.println("Decimal:="+ i);

Download this program.

Output of this program:

C:\corejava>java BinaryToDecimal
Enter the Binary value:

If you enjoyed this post then why not add us on Google+? Add us to your Circles

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: May 31, 2007

Related Tutorials

Discuss: Convert Binary to Decimal   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
May 22, 2013

public class bintodec{ public static void main(String args[]){ int i=8; int j=2; int k; String b=""; do{ k=i%j; i=i/j; b=k+b; } while (i!=0); System.out.println(b); } }
February 4, 2012
c programe

write a convert banary to decimal and visa versa
onin bula
June 19, 2012
student work

it's nice... i got the answer on this website thank you :)
December 4, 2012
Comp Science

Thank You Very Much for such a good java code.
December 4, 2012
Comp Science

Thank You Very Much for such a good java code.
Ram Chaudhary
August 21, 2013
Conversion Bin to Dec

Thanks for coding