we have byte string s = "1111000000000111000000000001000000000010010000000000000000000000"
(this value equal -1150951111012646912), if we use Long.parseLong(s, 2)
, we got "
java.lang.NumberFormatException: For input string: "1100000001000000010000000100000000001110000000010000010000000000" under radix 2".
i fixed this problem with this way to convert
new BigInteger(s, 2).longValue();
Explain please i can't understand what happend?
Same situation with this value:
s = 1100000001000000010000000100000000001110000000010000010000000000
(equal -4593600976060873728)
new BigInteger(s, 2).longValue();
You're passing in a size 64 binary string, which is bigger than the signed byte size of a long. Since
parseLong
doesn't accept unsigned binary representations and instead wants a - or (an optional) + in front of the number to denote the sign, it throws an error. The max byte length you can pass into parseLong is 63.An equivalent
parseLong
call to your first example would beLong.parseLong("-111111111000111111111110111111111101110000000000000000000000", 2)
, using the - sign and the two's complement of the number.