jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes String to Binary to Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to Binary to Integer" Watch "String to Binary to Integer" New topic

String to Binary to Integer

Brendan Crisler

Joined: Feb 07, 2007
Posts: 19
This may sound stupid, but I am needing help parsing out a 168-bit "binary" stirng (it's a string, but it's just 0s and 1s).

What I need to do is parse out certain portions of the string (using substring), and from those substrings, get an integer equivalent.


String a = testData.substring(8, 38);
// a = 001110101011000011010111011000

I want to convert 'a' to integer.

Any help is appreciated.


Bennett Rainville

Joined: Feb 16, 2007
Posts: 5
I don't know of anything in the java library that will do this for you, but if you'd like to try an implement a solution yourself this would be a good place to start.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Integer.parseInt(String ,int) does just what you want:

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Brendan Crisler

Joined: Feb 07, 2007
Posts: 19
Thanks. That's what I had done. Took me a while to realize it, and as soon as I did, I came here and saw the same things I had just done.

Thanks again.

Oliver Kamps

Joined: May 15, 2007
Posts: 18

since you have to handle 168 bits try java.math.BigInteger.BigInteger(String, int).

I agree. Here's the link: http://aspose.com/file-tools
subject: String to Binary to Integer
It's not a secret anymore!