# Convert Binary to Decimal

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

Tutorials

# 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.*; import java.io.*; public class BinaryToDecimal{   public static void main(String[] args) throws IOException{   BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));   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 != 0 && rem != 1){   System.out.println("This is not a binary number.");   System.out.println("Please try once again.");   System.exit(0);   }   }   int i= Integer.parseInt(str,2);   System.out.println("Decimal:="+ i);   } }```

`Output of this program:`

 C:\corejava>java BinaryToDecimal Enter the Binary value: 10010 Decimal:=18 C:\corejava>_

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

# Convert Binary to Decimal

Related Tutorials

Discuss: Convert Binary to Decimal   View All Comments

Subject (*):

Nikhil
May 22, 2013
SOL

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); } }
salman
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 :)
Junaid
December 4, 2012
Comp Science

Thank You Very Much for such a good java code.
Junaid
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