wood burning stoves*
The moose likes Java in General and the fly likes decoding binary arrays/boolean arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "decoding binary arrays/boolean arrays" Watch "decoding binary arrays/boolean arrays" New topic
Author

decoding binary arrays/boolean arrays

Leo Hassaine
Greenhorn

Joined: Aug 02, 2010
Posts: 25
I'm trying to come up with a way of decoding a long binary string into the appropriate characters.

Each value, 0-9 along with the operators + - * / and the numbers 25 and 50 are represented using 4 bits going from 0000 to 1111.

Here is the encoding:

Small Numbers:
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9

Operators:
1010 = +
1011 = -
1100 = *
1101 = /

Big Numbers:
1110 = 25
1111 = 50


Given a boolean array of size 64, whats the most efficient way of decoding it, 4 bits at a time, and making a new character array with the decoded values.

So for example, converting: (no spaces)

0001 1010 0111 1110 0111 1010 0110 1100 0011

into: (no spaces)

1 + 7 25 7 + 6 * 3

I've thought of using a bunch of if's or a database but thought the first way seems too simple and the second I've never done before.

Any help would be kindly appreciated
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

No offense, but why would you want to do this? Your binary string will always be longer than your original source string.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Rob Prime wrote: No offense, but why would you want to do this? Your binary string will always be longer than your original source string.

May be just simple encoding-decoding. In some sort of communication between 2 servers, just to make sure no one hacks off the data communicated and even if hacked, the hacker must know to decode it back - obviously he wont know. A way of protecting the data.
All of the above is just a 'May be' .. I dont know either.

Leo, first you need to split the Strings into 4 bits(May the Java API doc help you).
Consider having a map with your 4 bit as key and its corresponding value mapped.
Iterate through the split strings and do some kind of replace and keep appending it to a StringBuffer/StringBuilder.

OCPJP 6
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Do you really mean String as in "011101" or sequence of bytes - ie a byte[]?

Taking apart byte[] gets you involved with bitwise masking and shifting.

Bill
Leo Hassaine
Greenhorn

Joined: Aug 02, 2010
Posts: 25
Thanks for the replies

Rob Prime wrote:No offense, but why would you want to do this? Your binary string will always be longer than your original source string.


Genetic algorithms.

William Brogden wrote:Do you really mean String as in "011101" or sequence of bytes - ie a byte[]?

Taking apart byte[] gets you involved with bitwise masking and shifting.

Bill


I thought the most efficient way of storing 64 1's and 0's would be in a Boolean array of size 64. (Please correct me if this is wrong)



Vinoth Kumar Kannan wrote:
Rob Prime wrote: No offense, but why would you want to do this? Your binary string will always be longer than your original source string.

May be just simple encoding-decoding. In some sort of communication between 2 servers, just to make sure no one hacks off the data communicated and even if hacked, the hacker must know to decode it back - obviously he wont know. A way of protecting the data.
All of the above is just a 'May be' .. I dont know either.

Leo, first you need to split the Strings into 4 bits(May the Java API doc help you).
Consider having a map with your 4 bit as key and its corresponding value mapped.
Iterate through the split strings and do some kind of replace and keep appending it to a StringBuffer/StringBuilder.


Thanks for the help. I have no problem with the splitting of the boolean array, but am unsure as to how you create a map, as you mentioned, with corresponding values to each 4 bit key. If you could point me to any resources on such mapping techniques I'd greatly appreciate it.

Cheers,

Leo
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

You can use HashMap Class in java.util package, which allows you to store a key-value pair.
 
 
subject: decoding binary arrays/boolean arrays