Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 455
Daanish Avatar asked Jul 24 '13 12:07

Daanish


People also ask

What is BigInteger in Java?

BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java. lang. Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.


1 Answers

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);
like image 157
Juvanis Avatar answered Sep 22 '22 09:09

Juvanis