Reputation: 53
I just finish my own huffman coding program. It can perfectly encode and decode (return me a totally same file after decompression) The problem is that my result file after compression is larger than before.
For an instance, I compress a image file which is 1.8 MB, but my compressed file is about 14 MB. However after I decompress, it back to 1.8MB.
The data in my compressed file is:
How can I store Huffman tree more efficiently to make my compressed file smaller?
Upvotes: 0
Views: 2043
Reputation: 431
I'm assuming here that your tring to write your data to your file as a string of 1's
and 0's
. This means that each char is stored as 8 bits, and your file is larger. To convert a string of 1's
and 0's
to bytes try something like this (this is python, adjust to what language your using):
byte_array = bytearray()
for i in range(0, len(compressed), 8):
byte_array.append(int(compressed[i:i + 8], 2))
compressed
is the string of text you want to compress
We're looping over it in chunks of 8
For each chunck it gets converted to an integer in base 2, which corresponds to one byte. These bytes are added to an array.
For this to work, your original text sting has to be devisable by 8, so as theres nothing left on the last bit, so you might have to pad your orrional data with some extra characters.
Upvotes: 2