Folks, I am trying to parse an extremely long binary String to its decimal equivalent, but its throwing the NumberFormatException.
String s = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
"1111111111111111"; //String length is 1969
long n = Long.parseLong(s, 2); //line no. 25
System.out.println(n);
But it's giving the below-mentioned Runtime error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1111[...]111" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:592) at Check2.main(Check2.java:25)
Tried using BigInteger,valueOf() methods too, but all efforts are going in vain.
Please do let me know if there's any other way to achieve the desired result.
new BigInteger(s, 2). Yields (see next comment).n.bitLength()gives you the total number of bits from the first one bit. Subtract one to get the number of times you need to divide by 2.n.bitCount()gives you the number of one bits, so the number of times you need to subtract 1. So add those two numbers for a count of operations.BigInteger.