为什么这段代码会抛出NumberFormatException:

String binStr = "1000000000000000000000000000000000000000000000000000000000000000";
System.out.println(binStr.length());// =  64
System.out.println(Long.parseLong(binStr, 2));

最佳答案

1000000000000000000000000000000000000000000000000000000000000000大于Long.MAX_VALUE

参见https://stackoverflow.com/a/8888969/597657

考虑改用 BigInteger(String val, int radix)

编辑:

好的,这对我来说是新的。看来Integer.parseInt(binaryIntegerString, 2)Long.parseLong(binaryLongString, 2)将二进制解析为符号幅度而不是2的补码。

10-07 22:31