Solved

converting a long binary string to one large number in java

Posted on 2011-03-14
3
541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 27

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
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…

739 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