Re: Interesting Integer.parseInt() problem
- From: Thomas Hawtin <usenet@xxxxxxxxxxxxxxxxx>
- Date: Tue, 31 Jan 2006 02:53:38 +0000
Knute Johnson wrote:
I'm working on a project where I'm being given a string of 0s and 1s and need to mask them against another string of the same. I thought I would convert my string with Integer.parseInt("111",2) but I ran into a problem. You can't parse out 32 bits to an int if the MSB is 1. It throws a NumberFormatException. You can't parse -2147483648 in either radix 2, 8 or 16. My data is of course LSB first so 1 would be 100000000000000000000000000000000 and I can't parse it. Anyway, I thought it was interesting and wondered if anybody knows why this is so?
Presumably because you can't represent 1<<31 in a 32-bit signed number. Nor can int represent -2147483648 (base 16). -2147483648 contains digits not in base 2 or 8.
Although Integer.toBinaryString and friends create representations of the number as if it is unsigned, Integer.parseInt treats the value is normally.
If your data is LSB first, you'll need to reverse it, anyway. StringBuilder will do that nicely for you.
Tom Hawtin -- Unemployed English Java programmer http://jroller.com/page/tackline/ .
- References:
- Interesting Integer.parseInt() problem
- From: Knute Johnson
- Interesting Integer.parseInt() problem
- Prev by Date: Re: String replaceAll doesn't like commas
- Next by Date: Re: Java 1.6 Heisenbug involving startsWith
- Previous by thread: Re: Interesting Integer.parseInt() problem
- Next by thread: Re: Interesting Integer.parseInt() problem
- Index(es):
Relevant Pages
|