Re: Interesting Integer.parseInt() problem
- From: "opalpa@xxxxxxxxx opalinski from opalpaweb" <opalpa@xxxxxxxxx>
- Date: 30 Jan 2006 18:49:09 -0800
>>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@xxxxxxxxx
http://www.geocities.com/opalpaweb/
.
- References:
- Interesting Integer.parseInt() problem
- From: Knute Johnson
- Interesting Integer.parseInt() problem
- Prev by Date: Can ANT shut off or detect when a service is shut off?
- Next by Date: Re: String replaceAll doesn't like commas
- Previous by thread: Interesting Integer.parseInt() problem
- Next by thread: Re: Interesting Integer.parseInt() problem
- Index(es):
Relevant Pages
|
|