Java negative int for hex and reverse failures

public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE; String minHex = Integer.toHexString(Integer.MIN_VALUE); System.out.println(min + " " + minHex); System.out.println(Integer.parseInt(minHex, 16)); } } 

Gives

 -2147483648 80000000 Exception in thread "main" java.lang.NumberFormatException: For input string: "80000000" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:459) at Main3.main(Main3.java:7) 

What's happening?

+8
java decimal parsing signed hex
May 10 '09 at 12:17
source share
6 answers

It states that Integer.toHexString returns a string representation of the integer as an unsigned value - while Integer.parseInt accepts a signed int. If you use Integer.toString(value, 16) , you will get what you want.

+10
May 10 '09 at 12:54
source share

This is what always annoyed me. If you initialize an int with a hexadecimal literal, you can use the entire range of positive values โ€‹โ€‹up to 0xFFFFFF ; nothing more than 0x7FFFFF will really be a negative value. This is very convenient for masking bits and other operations in which you only care about the locations of the bits and not about their values.

But if you use Integer.parseInt () to convert a string to an integer, anything greater than "0x7FFFFFFF" is treated as an error. There is probably a good reason they did it, but itโ€™s still frustrating.

The simplest workaround is to use Long.parseLong () instead, and then convert the result to int.

 int n = (int)Long.parseLong(s, 16); 

Of course, you should only do this if you are sure that the number will be in the range Integer.MIN_VALUE..Integer.MAX_VALUE

+12
May 10 '09 at 2:05 p.m.
source share

According to the documentation, toHexString returns "a string representation of the integer argument as an unsigned integer at the base of 16."

So, the correct reverse operation is probably Integer.parseUnsignedInt , which was introduced as part of Java 8:

 public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE; String minHex = Integer.toHexString(Integer.MIN_VALUE); System.out.println(min + " " + minHex); System.out.println(Integer.parseUnsignedInt(minHex, 16)); } 
+3
Dec 06 '14 at 14:13
source share

Try the following:

 public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE; String minHex = Integer.toHexString(Integer.MIN_VALUE); System.out.println(min + " " + minHex); System.out.println(Integer.parseInt( "-" + minHex, 16)); } 

}

to get the following:

 -2147483648 80000000 -2147483648 
+2
May 10 '09 at
source share

You need to include a negative sign .

I do not have access to check this right now, but I would put it if you tried this value instead:

 Integer min = Integer.MIN_VALUE + 1; 

This will not bomb, but will give you a positive number (not negative) when you run ParseInt(min,16) .

The bit string does not actually have enough information to determine the character in this context, so you need to provide it. (consider the case when you use min = "F" . Is it +/- F? If you converted it to bits and saw 1111, and you knew that it was a byte, you can conclude that it is negative, but it is a lot, if.

+1
May 10 '09 at 12:20
source share

This seems to work for me:

 public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE; String minHex = Integer.toHexString(Integer.MIN_VALUE); System.out.println(min + " " + minHex); System.out.println((int)Long.parseLong(minHex, 16)); } } 

An integer is parsed as a "signed long" that processes such a large positive number, and then the character comes back, discarding it by "int".

0
Nov 27 '14 at 11:25
source share



All Articles