Interesting Integer.parseInt() problem

K

Knute Johnson

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);
}
}
 
O

opalpa

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
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
T

Thomas Hawtin

Knute said:
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
 
R

Roedy Green

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.
 
R

Roedy Green

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.

in the meantime, use long.parseLong. It will likely have the same
problem with Long.MIN_VALUE.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top