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?
Thanks,
knute...
public class test {
public static void main(String[] args) {
// String str = "1111111111111111111111111111111";
// String str = "7fffffff";
// String str = "17777777777";
int n = Integer.parseInt(str,2);
System.out.println(n);
}
}

Signature
Knute Johnson
email s/nospam/knute/
Thomas Hawtin - 31 Jan 2006 02:37 GMT
> 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
[quoted text clipped - 4 lines]
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
opalpa@gmail.com opalinski from opalpaweb - 31 Jan 2006 02:49 GMT
>From some experimenting it appears parseInt(n,16) is not an inverse of
Integer.toHexString() . The latter produces hex strings as one would
expect with negatives getting high order bit set and no sign. The
former accepts input that has an optional "-" followed by a necessarily
positive hex String, with one exception, the Integer.MIN_VALUE's hex
encoding 80000000 (So you can pass in "-80000000" and get
Integer.MIN_VALUE).
In other words the hex string to parseInt (after the optional sign)
needs be lower than or equal to 80000000 in unsigned terms. I did not
expect that.
Long story short parseInt is not not gonna do the deed.
package experiment;
public class test {
public static void main(String[] args) {
int n = 444;
String hex = Integer.toHexString(n);
System.out.println(""+n+" in hex is "+hex);
int back = Integer.parseInt(hex,16);
int negatively = Integer.parseInt("-"+hex,16);
System.out.println("back="+back+" negatively="+negatively);
}
}
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Roedy Green - 31 Jan 2006 04:45 GMT
On Mon, 30 Jan 2006 17:34:58 -0800, Knute Johnson
<nospam@ljr-2.frazmtn.com> wrote, quoted or indirectly quoted someone
who said :
> 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?
most likely because the author did not write a special case for it. A
typical parse routine creates an positive binary number then negates
it if it saw the sign. The algorithm won't work for a number without a
positive equivalent. It is a bug, since that number is legit.
nobody tested it since that number usually only comes up in binary
contexts.
see http://mindprod.com/jgloss/bugs.html
on how to report it.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 31 Jan 2006 04:52 GMT
On Tue, 31 Jan 2006 04:45:19 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :
>> It
>>throws a NumberFormatException. You can't parse -2147483648 in either
[quoted text clipped - 9 lines]
>nobody tested it since that number usually only comes up in binary
>contexts.
in the meantime, use long.parseLong. It will likely have the same
problem with Long.MIN_VALUE.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.