Can someone tell me the best way to decode binary data with variable length bit string in java?
For instance:
Binary data: 10101000 11100010 01100001 01010111 01110001 01010110
I may need to find the first match of any of the following 01, 100, 110, 1110, 1010 ...
In this case, the match will be 1010. Then I need to do the same for the rest of the binary data. Bit strings can be up to 16 bits long and cross byte boundaries.
Basically, I'm trying to use jpeg to decode Huffman using bit strings that I created from the Huffman tables in the headers. I can do this, only it is very dirty, I turn everything, including binary data, into Stringbuffers, and I know that this is the wrong way.
Before I loaded everything in string buffers, I tried using only numbers in binary format, but of course I cannot ignore leading 0s in code like 00011. I am sure there must be some smart way to use bit operators and would like to do it, but I was looking at pages explaining bit masks and left shifts, etc., and I still don't know!
Thanks for the help!
EDIT:
Thanks for all the suggestions. I went with a binary tree approach as it seems to be the standard way with Huffman stuff. It really makes sense, because Huffman codes are created using trees. I will also store binary data that I need to search in large integers. I donβt know how to indicate several answers correctly, but thanks anyway.
source share