This Tutorial helps you to know the code for Java binary to a decimal number.
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:
|