线程 "main" java.lang.NumberFormatException 中的异常:对于输入字符串:基数 16 下的“9000000000000000”

Exception in thread "main" java.lang.NumberFormatException: For input string: "9000000000000000" under radix 16

我尝试运行此代码,但出现错误。

System.out.println(Long.parseLong("9000000000000000", 16));

我们知道long的最小个数是-9,223,372,036,854,775,808,0x9000000000000000是-8,070,450,532,247,928,832为什么会报错?

参考Long#parseLong(String,int)

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, except that the first character may be a minus sign '-' ('\u002d') or plus sign '+' ('\u002B') provided that the string is longer than length 1.
  • The value represented by the string is not a value of type long.

Examples:
  parseLong("0", 10) returns 0L
  parseLong("473", 10) returns 473L
  parseLong("+42", 10) returns 42L
  parseLong("-0", 10) returns 0L
  parseLong("-FF", 16) returns -255L
  parseLong("1100110", 2) returns 102L
  parseLong("99", 8) throws a NumberFormatException
  parseLong("Hazelnut", 10) throws a NumberFormatException
  parseLong("Hazelnut", 36) returns 1356099454469L

16进制解析的十进制值为10376293541461622784,大于Long.MAX_VALUE(9223372036854775807),违反如下条件:

The value represented by the string is not a value of type long

因此抛出 NumberFormatException.

import java.math.BigInteger;

public class ParseLong {
    public static void main(String[] args) {
        System.out.println("Max Long value :" + Long.MAX_VALUE);
        System.out.println("Min Long value :" + Long.MIN_VALUE);
        System.out.println("Value without overflow " + new BigInteger("9000000000000000", 16));
        System.out.println("Value by parseUnsigned " + Long.parseUnsignedLong("9000000000000000", 16));
        System.out.println("Value by literal " + 0x9000000000000000L);
    }
}

9000000000000000 以 16 为底数是正数,因为没有符号。由于 long 是有符号的,它可以容纳的最大数字是 0x7FFF_FFFF_FFFF_FFFF。所以你的太厉害了。

如果你想要-8,070,450,532,247,928,832,使用parseUnsignedLong():

        System.out.println(Long.parseUnsignedLong("9000000000000000", 16));

输出:

-8070450532247928832

现在可以接受最大 0xFFFF_FFFF_FFFF_FFFF 的值。

Long.parseLong() 不会像算术那样“溢出”到负数 - 它不是解析位表示而是整数的数字。

16进制最大的long7FFFFFFFFFFFFFFF;你的价值比那个大

十进制比较:

Base 16            Decimal
7FFFFFFFFFFFFFFF    9,223,372,036,854,775,807
9000000000000000   10,376,293,541,461,622,784