Solved

converting a long binary string to one large number in java

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on link 14 46
PrintingFoundIt(Almost!(Damn!)) 1 26
VB: Convert 2 dates to specific format 24 48
List<PaisEntity> - show some 1 24
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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