Re: Interesting Integer.parseInt() problem



>>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/

.



Relevant Pages

  • Re: Is there a simpler way?
    ... Binary value or binary string? ... If you need to convert a hex string to a binary strings, ... static int setdigit; ... int SevStrHexToBin(const char *hexval, int groupchar, ...
    (microsoft.public.dotnet.languages.vc)
  • Re: CString hex value to CString decimal value
    ... P.S. you might what to change the return type to a long instead of an int. ... > and then put back as a string into an other CString. ... > that converting a large hex string to int does not work. ...
    (microsoft.public.vc.mfc)
  • Re: 0x00FF Arrg...
    ... I think you want a hex string. ... the & automatically promotes to int so does nothing. ... http://mindprod.com Java custom programming, consulting and coaching. ...
    (comp.lang.java.programmer)
  • Re: ? Identifying Current Language Code
    ... Here is a link about convern hex string into int, ... Convert Hex String to an Integer Value. ... - by Borland Developer Support Staff ...
    (microsoft.public.vc.mfc)
  • Re: convert hex string to hex
    ... Do you mean binary type? ... Convert hex string to int: ...
    (microsoft.public.sqlserver.programming)