How to convert binary value to decimal in Java


How to convert binary to decimal in java




public static int integerfrmbinary(String str){
double j=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)== '1'){
j=j+ Math.pow(2,str.length()-1-i);
}

}
return (int) j;
}


--------------------------------------------------------------------------------------

Other Way:


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);
}
}


run:
Enter the Binary value: 1010
Decimal:=10

--------------------------------------------

java - How to convert binary string value to decimal 
Java Convert Binary to Decimal,Binary to Decimal
How to convert binary to decimal?
Convert from binary to decimal?
HOW TO CONVERT BINARY TO DECIMAL USING JAVA PROGRAM
How do you convert a decimal number to binary in java?
How to write a java program that converts binary to
How to write a java program to convert binary to
Java program to convert binary,octal,decimal to
Java Program Convert Binary to Decimal
Decimal To Binary Converter - Java Tutorials
Binary To Decimal Conversion - Java
Thread: Binary-to-Decimal Conversion
Searches related to java Convert Binary to Decimal
javascript convert binary decimal
java convert hexadecimal decimal
java convert binary integer
java convert binary to decimal program
java program that converts binary to decimal
decimal to binary java code
java binary string to decimal
convert a number to binary java

Comments

Post a Comment