This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to Binary to Integer" Watch "String to Binary to Integer" New topic
Author

String to Binary to Integer

Brendan Crisler
Greenhorn

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.

Example:

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

I want to convert 'a' to integer.

Any help is appreciated.

Thanks,
B

Bennett Rainville
Greenhorn

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
Greenhorn

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
Greenhorn

Joined: May 15, 2007
Posts: 18
Hi,

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

Cheers,
Oliver
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String to Binary to Integer
 
Similar Threads
Reading a String backward
Ascii to binary and binary to ascii
Negative int
How to convert binary octet string into byte array for writing into socket
Wrapper class