You need to specify the radix. There's an overload of Integer#parseInt() which allows you to.
int foo = Integer.parseInt("1001", 2);
Answer from Matt Ball on Stack OverflowYou need to specify the radix. There's an overload of Integer#parseInt() which allows you to.
int foo = Integer.parseInt("1001", 2);
This might work:
public int binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
int result = 0;
for(int i=numbers.length - 1; i>=0; i--)
if(numbers[i]=='1')
result += Math.pow(2, (numbers.length-i - 1));
return result;
}
Java String to int binary number conversion - Stack Overflow
Convert binary to string in Java
[Help]Program to convert binary to integer
java - binary string to int - Stack Overflow
As explained above, Integer.toBinaryString() converts ~0 and ~1 to unsigned int so they will exceed Integer.MAX_VALUE.
You could use long to parse and convert back to int as below.
int base = 2;
for (Integer num : new Integer[] {~0, ~1}) {
String binaryString = Integer.toBinaryString(num);
Long decimal = Long.parseLong(binaryString, base);
System.out.println("INPUT=" + binaryString + " decimal=" + decimal.intValue()) ;
}
From http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#toBinaryString(int) : the toBinaryString() method converts its input into the binary representation of the "unsigned integer value is the argument plus 232 if the argument is negative".
From http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int) : the parseInt() method throws NumberFormatException if "The value represented by the string is not a value of type int".
Note that both ~0 and ~1 are negative (-1 and -2 respectively), so will be converted to the binary representations of 232-1 and 232-2 respectively, neither of which can be represented in a value of type int, so causing the NumberFormatException that you are seeing.
If 00111 is different from 111 then you can't store it as integers, you'll need to store them as strings.
Note however, that even in binary, 001112 is equal to 1112.
To parse a binary literal and store it in an integer, you use
Integer.parseInt(c, 2);
(but this doesn't solve the problem of keeping the leading zeros.)
You should use:
Integer.parseInt(c, 2)
Convert this from binary to a string for a special message. Share the code you used to decode and your answer below!
01001000 01100001 01110000 01110000 01111001 00100000 01000101 01100001 01110011 01110100 01100101 01110010 00100001
I have to make a program that asks the user for a binary number then converts the binary number into a integer, then it'll ask the user if they would like to enter another number or not.
import java.util.Scanner;
class binary { public static void main(String args[]) { String num; char n; String repeat;
Scanner in = new Scanner(System.in);
System.out.println("Enter a binary number: ");
num = in.nextLine();
n = num.charAt(0);
if (n != '1' && n != '0')
{
System.out.println("You did not enter a binary number..." +
"try again: ");
in.next();
}
else
{
System.out.println("Your binary number is " + Integer.parseInt(num,2));
}
}}
Above is what i have so far, I am having trouble asking the user if they would like to enter another number or not and also verifying the user input is a little buggy as well. If you can help me or point me in the right direction that would be great, thanks in advance.
Initialize a value to 0
Traverse the string from left to right as follows:
Shift the value one bit to the left - <<1
If the character is '1' add one - |0x1
try the following. As string is built in, its not possible to use it without using built in methods. ;)
String text =
long l = 0;
for(byte b: text.getBytes()) l = (l << 1) | (b & 1);
Use Integer.parseInt (see javadoc), that converts your String to int using base two:
int decimalValue = Integer.parseInt(c, 2);
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;
}
This piece of code I have written manually. You can also use parseInt as mentioned above . This function will give decimal value corresponding to the binary string :)