Java binary to decimal

This Tutorial helps you to know the code for Java binary to a decimal number.


 
Ads

Java binary to decimal

     

This Tutorial helps you to know the code for Java binary to a decimal number.

In this Tutorial we want to describe you a code that help you in understanding a how to get a 'Java binary to decimal'. For this we have a class BinarytoDecimal, Inside this class we have a main method that includes-

1)Buffered Reader   -  This is used to read a character stream from a file present in input stream reader.

The System.out .print  print  the binary specified  inside the braces.

2)readline ( )   - This is used to read the binary value entered by the user and store the value in a string variable s.

3)Integer.Parse ( )  -The integer class has a method parseInt( ) that returns you the integer value from  string passed as argument representation. 

Finally the System.out.println print the decimal value from the binary value. 

BinarytoDecimal.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BinarytoDecimal {

    public static void main(String[] args) throws Exception {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the binary value:");
         String s = br.readLine();
        System.out.println("Decimal value is : "+Integer.parseInt(s, 2));

    }
}

Output of the program

  Enter the binary value:
110111
Decimal value is : 55

Download Source code

Advertisements

Share on Google+Share on Google+

Java binary to decimal

Posted on: November 6, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Java binary to decimal  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
mhira
January 13, 2012
decimal to binary 210 code

ineed a code
Nawras
March 5, 2012
thank you

I didn't know that parseInt() can do this conversion between number formats, thanks
Edgar
March 18, 2012
BInary to Decimal

import java.util.Scanner; public class BintoDec { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int bin, dec=0, pos=1, f; bin = scan.nextInt(); while(bin>0){ f=bin%10; dec+=pos*f; pos*=2; bin/=10; } System.out.println(dec); } }

Ads

 

Ads