I have an array of Strings that represent Binary numbers (without leading zeroes) that I want to convert to their corresponding base 10 numbers. Consider:
bi
You need to specify the radix. There's an overload of Integer#parseInt()
which allows you to.
int foo = Integer.parseInt("1001", 2);
Fixed version of java's Integer.parseInt(text) to work with negative numbers:
public static int parseInt(String binary) {
if (binary.length() < Integer.SIZE) return Integer.parseInt(binary, 2);
int result = 0;
byte[] bytes = binary.getBytes();
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == 49) {
result = result | (1 << (bytes.length - 1 - i));
}
}
return result;
}
int foo = Integer.parseInt("1001", 2);
works just fine if you are dealing with positive numbers but if you need to deal with signed numbers you may need to sign extend your string then convert to an Int
public class bit_fun {
public static void main(String[] args) {
int x= (int)Long.parseLong("FFFFFFFF", 16);
System.out.println("x =" +x);
System.out.println(signExtend("1"));
x= (int)Long.parseLong(signExtend("1"), 2);
System.out.println("x =" +x);
System.out.println(signExtend("0"));
x= (int)Long.parseLong(signExtend("0"), 2);
System.out.println("x =" +x);
System.out.println(signExtend("1000"));
x= (int)Long.parseLong(signExtend("1000"), 2);
System.out.println("x =" +x);
System.out.println(signExtend("01000"));
x= (int)Long.parseLong(signExtend("01000"), 2);
System.out.println("x =" +x);
}
private static String signExtend(String str){
//TODO add bounds checking
int n=32-str.length();
char[] sign_ext = new char[n];
Arrays.fill(sign_ext, str.charAt(0));
return new String(sign_ext)+str;
}
}
output:
x =-1
11111111111111111111111111111111
x =-1
00000000000000000000000000000000
x =0
11111111111111111111111111111000
x =-8
00000000000000000000000000001000
x =8
I hope that helps!
static int binaryToInt (String binary){
char []cA = binary.toCharArray();
int result = 0;
for (int i = cA.length-1;i>=0;i--){
//111 , length = 3, i = 2, 2^(3-3) + 2^(3-2)
// 0 1
if(cA[i]=='1') result+=Math.pow(2, cA.length-i-1);
}
return result;
}
For me I got NumberFormatException when trying to deal with the negative numbers. I used the following for the negative and positive numbers.
System.out.println(Integer.parseUnsignedInt("11111111111111111111111111110111", 2));
Output : -9
I love loops! Yay!
String myString = "1001001"; //73
While loop with accumulator, left to right (l
doesn't change):
int n = 0,
j = -1,
l = myString.length();
while (++j < l) n = (n << 1) + (myString.charAt(j) == '0' ? 0 : 1);
return n;
Right to left with 2 loop vars, inspired by Convert boolean to int in Java (absolutely horrible):
int n = 0,
j = myString.length,
i = 1;
while (j-- != 0) n -= (i = i << 1) * new Boolean(myString.charAt(j) == '0').compareTo(true);
return n >> 1;
A somewhat more reasonable implementation:
int n = 0,
j = myString.length(),
i = 1;
while (j-- != 0) n += (i = i << 1) * (myString.charAt(j) == '0' ? 0 : 1);
return n >> 1;
A readable version :p
int n = 0;
for (int j = 0; j < myString.length(); j++) {
n *= 2;
n += myString.charAt(j) == '0' ? 0 : 1;
}
return n;