Integer.valueOf(String, int radix) and Integer.parseInt(String, int radix) will only analyze numbers from -2,147,483,648 to 2,147,483,647, that is, the values โโof 32-bit signed integers.
These functions cannot interpret two complement numbers for binary ( radix = 2 ), because the transmitted string can be of any length, and therefore the leading 1 can be part of a number or a sign bit. I think the Java developers decided that the most logical way is to never accept two additions, and not assume that the 32nd bit is a signed bit.
They read your binary input string as unsigned 3,549,763,730 (more than max int value). To read a negative value, you must give a positive binary number with a - in front. For example, for -5 :
Integer.parseInt("1011", 2); // 11 // Even if you extended the 1s to try and make two complement of 5, // it would always read it as a positive binary value Integer.parseInt("-101", 2); // -5, this is right
Solutions:
I suggest, firstly, that if you can save it as a positive number with additional information about the sign yourself (for example, the - symbol), do it. For instance:
String binString; if(i < 0) binString = "-" + Integer.toBinaryString(-i); else
If you need to use signed binary strings to accept a negative binary number of two additions (like a string) and parse it into int, I suggest you take two additions manually, convert them to int, and then correct the sign. Recall that two additions = one addition + 1, and one addition just reverses each bit.
As an example implementation:
String binString = "11010011100101010001100010010010"; StringBuilder onesComplementBuilder = new StringBuilder(); for(char bit : binString.toCharArray()) { // if bit is '0', append a 1. if bit is '1', append a 0. onesComplementBuilder.append((bit == '0') ? 1 : 0); } String onesComplement = onesComplementBuilder.toString(); System.out.println(onesComplement); // should be the NOT of binString int converted = Integer.valueOf(onesComplement, 2); // two complement = one complement + 1. This is the positive value // of our original binary string, so make it negative again. int value = -(converted + 1);
You can also write your own version of Integer.parseInt for 32-bit binary numbers with two additions. This, of course, assumes that you are not using Java 8 and cannot simply use Integer.parseUnsignedInt , as @llogiq pointed out when I introduced this.
EDIT: First you can use Long.parseLong(String, 2) , and then calculate two additions (and mask it by 0xFFFFFFFF), and then lower long to int . Faster to write, possibly faster code.