Java,Long.parse 二进制字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14926920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:53:01  来源:igfitidea点击:

Java, Long.parse binary String

javalong-integer

提问by Mickey Tin

Why does this code throw a NumberFormatException:

为什么这段代码会抛出一个NumberFormatException

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

采纳答案by Eng.Fouad

1000000000000000000000000000000000000000000000000000000000000000is larger than Long.MAX_VALUE.

1000000000000000000000000000000000000000000000000000000000000000大于Long.MAX_VALUE

See https://stackoverflow.com/a/8888969/597657

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

Consider using BigInteger(String val, int radix)instead.

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



EDIT:

编辑:

OK, this is new for me. It appears that Integer.parseInt(binaryIntegerString, 2)and Long.parseLong(binaryLongString, 2)parse binary as sign-magnitude not as a 2's-complement.

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

回答by Oliver Charlesworth

Because it's out of range. 1000...000is 263, but Longonly goes up to 263- 1.

因为超出范围了。 1000...000是 2 63,但Long只会上升到 2 63- 1。

回答by jlordo

This is the same for all of Long, Integer, Shortand Byte. I'll explain with a Byteexample because it's readable:

这是所有的相同LongIntegerShortByte。我会用一个Byte例子来解释,因为它是可读的:

System.out.println(Byte.MIN_VALUE); // -128
System.out.println(Byte.MAX_VALUE); // 127
String positive =  "1000000"; // 8 binary digits, +128 
String negative = "-1000000"; // 8 binary digits, -128
String plus     = "+1000000"; // 8 binary digits, +128
Byte.parseByte(positive, 2); //will fail because it's bigger than Byte.MAX_VALUE 
Byte.parseByte(negative, 2); //won't fail. It will return Byte.MIN_VALUE
Byte.parseByte(plus, 2);     //will fail because its bigger than Byte.MAX_VALUE

The digits are interpreted unsigned, no matter what radix is provided. If you want a negative value, you have to have the minus sign at the beginning of the String. JavaDocsays:

无论提供什么基数,数字都被解释为无符号。如果你想要一个负值,你必须在字符串的开头有一个减号。JavaDoc说:

Parses the string argument as a signed long in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int)returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D')to indicate a negative value or an ASCII plus sign '+' ('\u002B')to indicate a positive value. The resulting long value is returned.

将字符串参数解析为第二个参数指定的基数中的有符号长整型。字符串中的字符必须都是指定基数的数字(由是否Character.digit(char, int)返回非负值决定),除了第一个字符可以是ASCII减号'-' ('\u002D')表示负值或ASCII加号'+' ('\u002B')表示正值.返回结果 long 值。

In order to get MAX_VALUEwe need:

为了得到MAX_VALUE我们需要:

String max  =  "1111111"; // 7 binary digits, +127 
// or
String max2 = "+1111111"; // 7 binary digits, +127 

回答by Evgeniy Dorofeev

This is because Long.parseLong cannot parse two's complement representation. The only way to parse two's complement binary string representation in Java SE is BigInteger:

这是因为 Long.parseLong 无法解析二进制补码表示。在 Java SE 中解析二进制补码二进制字符串表示的唯一方法是 BigInteger:

long l = new BigInteger("1000000000000000000000000000000000000000000000000000000000000000", 2).longValue()

this gives expected -9223372036854775808result

这给出了预期的 -9223372036854775808result

回答by SmRndGuy

Largest long value is actually:

最大的 long 值实际上是:

0111111111111111111111111111111111111111111111111111111111111111b = 9223372036854775807

回答by Enrico Giurin

This is the largest possible long (9223372036854775807 = 2 exp 63 - 1) in binary format. Note the L at the end of the last digit.

这是二进制格式的最大可能长度 (9223372036854775807 = 2 exp 63 - 1)。注意最后一位数字末尾的 L。

 long largestLong = 0B0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L;