how to convert a binary to a decimal?

Given one string consisting of numbers which actually corresponds to a real big binary number(>0), how to convert it to a decimal string in java. For example,

"10010001000111110110110100010100001001100100101001010001100000011101111110011101000101101111101001101101111100110001100011001001101001" in binary =
"12345678910111213141516171819202122232425" in decimal
jl66Asked:
Who is Participating?
 
dpearsonCommented:
Check out the BigInteger class:
http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html

One of the constructors takes a string and a "radix" which is the fancy term for base, so for binary this is 2:

            BigInteger bi = new BigInteger("100101000111111110000", 2);
                bi.toString() ; // Print it in Decimal

Hope that helps,

Doug
0
 
jl66Author Commented:
Revised title.
0
 
jl66Author Commented:
If we restrict the binary number to be <=2^128, is it a bit easy to resolve it?
0
 
dpearsonCommented:
BigInteger handles arbitrary sized numbers.  So you should be fine with any sized binary.
0
 
jl66Author Commented:
Thanks a lot. Very helpful.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.