【发布时间】:2015-11-23 08:09:33
【问题描述】:
谁能告诉我为什么这段代码会抛出异常?
int value = 0xabcdef01;
System.out.println(value); // prints -1412567295
String hex = Integer.toHexString(value);
System.out.println(hex); // prints abcdef01
// why does this line fail?
Integer.parseInt(hex, 16);
此代码引发以下异常:
java.lang.NumberFormatException: For input string: "abcdef01"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
我正在使用以下 JDK 在 Windows 7 上运行
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
【问题讨论】:
-
@nafas 感谢该链接似乎直接相关
-
np mate,顺便说一句,我只是尝试这样做以确保使用
Long.parseLong(hex,16);可以正常工作。
标签: java integer hex numberformatexception