Solved

how to convert a binary to a decimal?

Posted on 2014-01-07
5
267 Views
Last Modified: 2014-01-09
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
0
Comment
Question by:jl66
  • 3
  • 2
5 Comments
 

Author Comment

by:jl66
ID: 39763488
Revised title.
0
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 39764271
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
 

Author Comment

by:jl66
ID: 39764277
If we restrict the binary number to be <=2^128, is it a bit easy to resolve it?
0
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 500 total points
ID: 39764289
BigInteger handles arbitrary sized numbers.  So you should be fine with any sized binary.
0
 

Author Closing Comment

by:jl66
ID: 39769674
Thanks a lot. Very helpful.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question