Solved

converting a long binary string to one large number in java

Posted on 2011-03-14
3
505 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

25 Experts available now in Live!

Get 1:1 Help Now