Solved

converting a long binary string to one large number in java

Posted on 2011-03-14
3
492 Views
Last Modified: 2012-05-11
hello good folks,
 i have string of binary data that i need to convert to a large int.  the 1s and 0s are saved into a string object and i want to convert them into integers and then into one BigInteger.  the language of choice is java.  thank you in advance
0
Comment
Question by:gmakungo22
3 Comments
 
LVL 1

Expert Comment

by:pjbaratelli
ID: 35132441
It's been awhile since I Java'd, but what about this...

String binaryString = "1001";
int base = 2;
int decimal = Integer.parseInt(binaryString, base);
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35135080
I'd do it as a loop since it works in any language
string str = "100101"
long num = 0;

for(int i = 0; i < str.length(); ++i)
{
  if(str[i] == 1)
    num +=1;
  if(i != str.length()-1)
    num *= 2;
}

Open in new window

0
 
LVL 26

Accepted Solution

by:
dpearson earned 125 total points
ID: 35135269
I think you want to use:

BigInteger result = new BigInteger(binaryString, 2) ;

This uses the form of BigInteger that support reading a string representation in an arbitrary radix - which for binary is 2.

Details here:
http://download.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html#BigInteger%28java.lang.String,%20int%29

Doug
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now