Java负int到十六进制并返回失败 [英] Java negative int to hex and back fails
本文介绍了Java负int到十六进制并返回失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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));
code
$ b $ p
-2147483648 80000000
线程main中的异常java.lang.NumberFormatException:对于输入字符串:80000000
at java.lang.NumberFormatException .forInputString(NumberFormatException.java:48)$ b $ at java.lang.Integer.parseInt(Integer.java:459)
at Main3.main(Main3.java:7)
Whats up?
解决方案它被记录为 Integer.toHexString
返回整数的字符串表示形式作为无符号值 - 而 Integer.parseInt
需要一个签名int。如果你使用 Integer.toString(value,16)
,你可以得到你想要的。
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)
Whats up?
解决方案 It's documented that Integer.toHexString
returns a string representation of the integer as an unsigned value - while Integer.parseInt
takes a signed int. If you use Integer.toString(value, 16)
instead you'll get what you want.
这篇关于Java负int到十六进制并返回失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文