2016-01-23 59 views
0

考虑到十六进制中的FFFFFFB2十进制为-78解析Java中的十六进制

为什么我在此操作时出错?

Integer.parseInt("FFFFFFB2", 16) 

错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "FFFFFFFFFFFFFFB2" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:495) 
    at tmp.Test.main(Test.java:11) 
+1

您是否知道您尝试转换的数字是一个庞大的数字,相当于18446744073709552000的十进制数? – SMA

回答

1

taking in account that FFFFFFFFFFFFFFB2 in HEX is -78 in decimal"

Nope,FFFFFFFFFFFFFFB2十六进制是18446744073709552000十进制。你错误的十六进制补码。

如果你想利用二进制补位模式,并把它转换成一个数字,this answer建议一招:

long l = new BigInteger("FFFFFFFFFFFFFFB2",16).longValue(); 

And it actually works

注意我使用BigIntegerlong,而不是longint,因为数字太大。

0

按照documentation

An exception of type NumberFormatException is thrown if any of the following situations occurs:

The value represented by the string is not a value of type int.

前面已经指出的那样,你的号码是不是int