I’m just beginning to learn about file compression and I’ve run into a bit of a roadblock. I have an application that will encode a string such as “program” as a compressed binary representation "010100111111011000"(note this is still stored as a String).
Encoding
g 111
r 10
a 110
p 010
o 011
m 00
Now I need to write this to the file system using a FileOutputStream, the problem I’m having is, how can I convert the string “010100111111011000” to a byte[]/bytes to be written to the file system with FileOutputStream?
I’ve never worked with bits/bytes before so I’m kind of at a dead end here.
An introduction to bit-shift operators:
First, we have the left-shift operator,
x << n. This will shift all the bits inxleft bynbits, filling the new bits with zero:Next, we have the signed right-shift operator,
x >> n. This shifts all the bits inxright by n, copying the sign bit into the new bits:Finally, we have the zero-fill right-shift operator,
x >>> n. This shifts all bits inxright bynbits, filling the new bits with zero:You may also find useful the bitwise-or operator,
x | y. This compares the bits in each position inxandy, setting the new number’s bit on if it was on in eitherxory, off otherwise:You should only need the previous operators for the problem at hand, but for the sake of completeness, here are the last two:
The bitwise-and operator,
x & ysets the bits in the output to one if and only if the bit is on in bothxandy:The bitwise-xor operator,
x ^ ysets the output bits to one if the bit is on in one number or the other but not both:Now, applying these to the situation at hand:
You will need to use the bit-shift operators to add and manipulate bits. Start setting bits at the right side according to their string representations and shift them over. Continue until you hit the end of a byte, and then move to the next byte. Say we want to create a byte representation of “1100 1010”:
I will, of course, leave it to you to apply this to your work.