How do you convert A binary number to a BigInteger in Java?

Daanish picture Daanish · Jul 24, 2013 · Viewed 9.9k times · Source

I needed to convert a very big binary value into its decimal equivalent. As it is a big integer I was using BigInteger. So how do I convert this binary number to a BigInteger?

Answer

Juvanis picture Juvanis · Jul 24, 2013

If you have the String representation of your binary number, provide it to this overloaded BigInteger constructor to create an instance:

BigInteger(String val, int radix);

In your case, radix is clearly 2, i.e. you can use something like this:

BigInteger yourNumber = new BigInteger("101000101110...1010", 2);