19

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?

Daanish
  • 1,061
  • 7
  • 18
  • 29

3 Answers3

25

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);
Juvanis
  • 25,802
  • 5
  • 69
  • 87
5

If you have binary String you can convert it to BigInteger like this:

 String binaryString = "1010110101011010101010101101010101100101011010001010001100101110";
 BigInteger bigInt = new BigInteger(binaryString, 2);
Juvanis
  • 25,802
  • 5
  • 69
  • 87
amicngh
  • 7,831
  • 3
  • 35
  • 54
3
    String binaryValue = "11111111";
    BigInteger bi = new BigInteger(binaryValue, 2);  
Java SE
  • 2,073
  • 4
  • 19
  • 25