2

So I have this binary string (32 chars corresponding to 32 bits):

 String s = "11111111110000011101110111011000";

When I try to convert it to a number, it throws a NumberFormatException:

 Integer.parseInt(s,2);

Could not figure out why. Any idea? The strange thing is if I replace the first bit in the string from the left (1) with a zero bit (0), then it works. The first bit is only for indicating the sign of the number, so I should not make any overflow of 4 bytes in here, right?

=================

UPDATED QUESTION: Thanks to everyone for the answers. I got it now; basically the parseInt() method CANNOT handle the sign bit and it assumes that the sign bit just represents the value as any other bit. So now the question is how to I convert such binary string to a signed INTEGER number? I know some of you suggested I use long, but in my situation I just need it to be an int (long to explain, but you can assume that).

2
  • because its binary, not an integer. if it were 1011, you'd expect 11, but you'd get 1,011 Commented Jun 3, 2015 at 4:02
  • "long to explain" I see what you did there :P Commented Jun 3, 2015 at 4:21

4 Answers 4

3

The number that you're trying to parse is bigger than 2^31-1 (max integer), use Long instead:

String s = "11111111110000011101110111011000";
System.out.println(Long.parseLong(s, 2)); // 4,290,895,320

If you want to see what's the maximum value of an Integer you can do:

System.out.println(Integer.MAX_VALUE); // 2,147,483,647

Responding your UPDATE section, if you don't mind loosing information (which happens when you convert long to int) you can do:

String s = "11111111110000011101110111011000";
long l = Long.parseLong(s,2);
int i = (int)l;
System.out.println(i); // -4071976
Sign up to request clarification or add additional context in comments.

8 Comments

Can you explain why it is bigger than 2^32 while the first bit (from the left) is only used to indicate the sign of the number?
@Simo not sure if I got what you asked - but I added a part to the answer that might explain it.
Hey alfasin. Got your answer. Thanks. Any idea of how to convert such binary string to an int (not to a long). It has something to do with my encoding for compression, so long numbers will not help with compression.
@Simo you cannot convert it to int without losing information. If you're okay with losing information then you can check the length of s and remove characters until you have a string of length 30 (you should rather avoid strings of length 31 - just to avoid the edge case of hitting 2^31). One way to do it would be as I stated above in the additional example.
He's not losing information - he has 32 bits, and wishes to interpret them as a signed int.
|
2

You are trying to parse a 32-bit number into an Integer, which can only have 31 bits and a sign. parseInt can't handle the sign bit; you need to give the sign as + or - (or nothing). Your number should be (if I haven't screwed up my conversion) -4071976, which can be parsed if you give it to parseInt as -1111100010001000101000, but not as 11111111110000011101110111011000.

2 Comments

Got it. Thanks. Now my question is how to convert that binary string to an int number? In my app (has to do with encoding), I need it to be an int, so the first bit needs to represent the sign.
(int)Long.parseLong(s, 2) is -4071976 (as a signed int as you want, not as long).
2

As other people already mentioned, this binary string does not fit the 32-bit singed int. However since Java 8 you can parse it as unsigned int, which works perfectly!

String s = "11111111110000011101110111011000";
System.out.println(Integer.parseUnsignedInt(s, 2)); // -4071976

There are other useful unsigned math methods added in Java 8 as well.

1 Comment

Nice! I wasn't aware of this option!
0

You are telling that you are trying to parse binay String

String s = "11111111110000011101110111011000";

But this value is out of Java integer range:

Integer is 32 bit and for Java, the range is -2,147,483,648 to 2,147,483,647. But your input has the value which is out of the range. So you are getting the exception.

But long is having 64 bit and the range is –9,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807. So you can use the below:

String s = "11111111110000011101110111011000";
Long.parseLong(s,2);//4290895320

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.