I'm trying to convert a couple of binary strings back to int. However it doesn't convert all my binary strings, leaving me a java.lang.NumberFormatException exception. Here is my test code with 3 binary string:
public class Bin {
public static void main(String argvs[]) {
String binaryString ;
binaryString = Integer.toBinaryString(~0);
//binaryString = Integer.toBinaryString(~1);
//binaryString = "1010" ;
int base = 2;
int decimal = Integer.parseInt(binaryString, base);
System.out.println("INPUT=" + binaryString + " decimal=" + decimal) ;
}
}
If I convert the "1010" it works great, but when I try to convert one of the other two I get the exception. Can someone explain to me why this is ?
Cheers
Integer.toBinaryStringis unsigned andInteger.parseIntis signed.