• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

decoding binary arrays/boolean arrays

 
Leo Hassaine
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense, but why would you want to do this? Your binary string will always be longer than your original source string.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use HashMap Class in java.util package, which allows you to store a key-value pair.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic