Why does this code throw a NumberFormatException :
String binStr = "1000000000000000000000000000000000000000000000000000000000000000";
System.out.println(binStr.length());// = 64
System.out.println(Long.parseLong(binStr, 2));
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
1000000000000000000000000000000000000000000000000000000000000000is larger thanLong.MAX_VALUE.See https://stackoverflow.com/a/8888969/597657
Consider using
BigInteger(String val, int radix)instead.EDIT:
OK, this is new for me. It appears that
Integer.parseInt(binaryIntegerString, 2)andLong.parseLong(binaryLongString, 2)parse binary as sign-magnitude not as a 2’s-complement.