java - 将十六进制字符串解析为 int 值时出现意外的 NumberFormatException

标签 java hex

我想解析一个包含 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 than Character.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/

相关文章:

java - Calendar类型的set(int,int)方法不适用于参数(int,String)

java - Java 中的十六进制数据类型

java - 这段代码如何生成该十六进制值?

javascript - NodeJS Forge - 将加密字节字符串转换为十六进制字符数组

java - Cassandra JDBC 是否提供数据源?

java - 使用Java代码进行Jmeter分布式测试

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException | JAVA

java - Thymeleaf:更新表单提交表

ios - 将字节命令发送到 Star Micronics SM-S220I 打印机

c# - 将 int 转换为带前导零的十六进制