I try to parse Integer.MIN_VALUE from hex to int, but I get a NumberFormatException. When I add an minus to the String it is working.
Is this a bug or do I misunderstand something. From my point of view encoding and decoding should be bijective. But it appears to be not.
I have to decode "0x80000000". How do I do it? I could catch the exception and add a minus to the String and retry. But this appears not clean to me.
Here comes a running example:
public static void main(String[] args) {
int i1 = Integer.MIN_VALUE; //0x80000000
String s1 = Integer.toHexString(i1);
String s2 = "-" + s1;
System.out.println(String.format("Out1: %1$d | %1$h == %2$s <> %3$s", i1 , s1, s2));
// Out1: -2147483648 | 80000000 == 80000000 <> -80000000
// this should work, but does not
try {
int s1_parsed = Integer.parseInt(s1, 16);
System.out.println(String.format("Out2: %1$d | %1$h, %2$d | %2$h", i1, s1_parsed));
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
// this is working, but I do not know why
try {
int s2_parsed = Integer.parseInt(s2, 16);
System.out.println(String.format("Out3: %1$d | %1$h == %2$d | %2$h", i1, s2_parsed));
// Out3: -2147483648 | 80000000 == -2147483648 | 80000000
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
}