我想解析一个包含 8 个十六进制数字(4 个字节)的字符串,但出现 NumberFormatException。这里出了什么问题?
assertThat(Integer.parseInt("FFFF4C6A",16),is(0xFFFF4C6A));
最佳答案
您的数字代表的数字大于可分配给 int 的数字。尝试:
Long.parseLong("FFFF4C6A", 16);
给出 4294921322。
来自doc :
An exception of type
NumberFormatException
is thrown if any of the following situations occurs:
- The first argument is null or is a string of length zero.
- The radix is either smaller than
Character.MIN_RADIX
or larger thanCharacter.MAX_RADIX
.- Any character of the string is not a digit of the specified radix, …
- The value represented by the string is not a value of type int.
这是您遇到的第四个案例。
关于java - 将十六进制字符串解析为 int 值时出现意外的 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12403212/