英文:
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,为什么会出现错误呢?
英文:
I try to run this code but it occur error.
System.out.println(Long.parseLong("9000000000000000", 16));
As we know that the minimum number of long is -9,223,372,036,854,775,808 and 0x9000000000000000 is -8,070,450,532,247,928,832 why it will occur error?
答案1
得分: 10
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。
英文:
9000000000000000 base 16 is a positive number since there is no sign. Since a long
is signed, the greatest number that it can hold is 0x7FFF_FFFF_FFFF_FFFF. So yours is too great.
If you want -8,070,450,532,247,928,832, use parseUnsignedLong()
:
System.out.println(Long.parseUnsignedLong("9000000000000000", 16));
Output:
> -8070450532247928832
Now values up to 0xFFFF_FFFF_FFFF_FFFF are accepted.
答案2
得分: 2
关于 Long#parseLong(String,int) 的解释:
如果出现以下任何情况,将抛出 NumberFormatException 异常:
- 第一个参数为 null 或长度为零的字符串。
- 基数小于 Character.MIN_RADIX 或大于 Character.MAX_RADIX。
- 字符串中的任何字符不是指定基数的数字,但如果字符串长度大于1,则第一个字符可以是减号 '-' ('\u002d') 或加号 '+' ('\u002B')。
- 字符串表示的值不是 long 类型的值。
示例:
- parseLong("0", 10) 返回 0L
- parseLong("473", 10) 返回 473L
- parseLong("+42", 10) 返回 42L
- parseLong("-0", 10) 返回 0L
- parseLong("-FF", 16) 返回 -255L
- parseLong("1100110", 2) 返回 102L
- parseLong("99", 8) 抛出 NumberFormatException
- parseLong("Hazelnut", 10) 抛出 NumberFormatException
- parseLong("Hazelnut", 36) 返回 1356099454469L
使用基数 16 解析的十进制值为 10376293541461622784,大于 Long.MAX_VALUE(9223372036854775807),违反以下条件:
- 字符串表示的值不是 long 类型的值
因此会抛出
NumberFormatException
。
使用基数 16 解析的十进制值为 10376293541461622784,大于 Long.MAX_VALUE(9223372036854775807),违反以下条件:
字符串表示的值不是 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);
}
}
英文:
Referring to 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
The decimal value parsed using radix 16 is 10376293541461622784 which is greater than Long.MAX_VALUE(9223372036854775807), violate following condition:
>The value represented by the string is not a value of type long
hence throwing 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);
}
}
答案3
得分: 1
Long.parseLong()
不会像算术运算一样“溢出”到负数 - 它不是解析位表示,而是整数的数字。
十六进制中最大的long
是7FFFFFFFFFFFFFFF
;您的值比这还要大。
以十进制进行比较:
十六进制 十进制
7FFFFFFFFFFFFFFF 9,223,372,036,854,775,807
9000000000000000 10,376,293,541,461,622,784
英文:
Long.parseLong()
does not "overflow" around to a negative number like arithmetic does - it's not a parsing a bit representation but rather the digits of a whole number.
The largest long
in base 16 is 7FFFFFFFFFFFFFFF
; your value is bigger than that.
For comparison in decimal:
Base 16 Decimal
7FFFFFFFFFFFFFFF 9,223,372,036,854,775,807
9000000000000000 10,376,293,541,461,622,784
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论