Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

converting a long binary string to one large number in java

Posted on 2011-03-14
3
Medium Priority
?
573 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 28

Accepted Solution

by:
dpearson earned 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Loops Section Overview

885 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