将十六进制字符串转换为整数时出现java.lang.NumberFormatException异常

6
我想将十六进制字符串转换为十进制,但在以下代码中出现了错误:
String hexValue = "23e90b831b74";       
int i = Integer.parseInt(hexValue, 16);

错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "23e90b831b74"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:495)
1个回答

14

23e90b831b74太大了,无法适应int

通过计算数字,您可以轻松地看到这一点。 十六进制数字中的每两个数字需要一个字节,因此12个数字需要6个字节,而int只有4个字节。

使用Long.parseLong

String hexValue = "23e90b831b74";       
long l = Long.parseLong(hexValue, 16);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接