This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes string to byte array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "string to byte array problem" Watch "string to byte array problem" New topic

string to byte array problem

Ravindra Harige
Ranch Hand

Joined: Jun 10, 2005
Posts: 33
I've long sequence of 0 and 1's in a string and i want this string to be stored in a byte array,but the size of byte array should be
( [number of 0's and 1's in string]/8 )
for example,if i've 80 consecutive 0's and 1's in a string then i want a byte array of size 10 consisting of the exact sequence of 0's and 1's as in string.
please help me on how to go about this..

Rob Spoor

Joined: Oct 27, 2005
Posts: 19672

First of all you must decide what to do if the length is not a multiple of 8. Do you add zeros at the start? At the end? Something else?

But lets suppose you've solved that, and the string is now in the right format. You can use something similar to this:

[ March 24, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: string to byte array problem