Huffman compress file (Got the tree but can't compress)- Java -


alright trying file compress using huffman tree.

we got tree working fine unable figure out how write binary string file.

so example our tree returns: '110', should mean byte: '00000110' right?

and if returns: '11111111 11111110' should mean what? should write in in byte?

so question how convert binary string bytes can write on file?

thanks alot, ara

so example our tree returns: '110', should mean byte: '00000110' right?

wrong. should have byte buffer of bits write bits. write 3 bits 110 byte. (you need decide on convention bit ordering in byte.) still have 5 unused bits in byte, there sits. write 10 buffer. byte buffer has 11010, , 3 unused bits. still sits. try write 111011 byte buffer. first 3 bits go byte buffer, giving 11010111. have filled buffer, write out byte file. left 011. clear byte buffer of bits since wrote out, , put in remaining 011 last code. byte buffer has 3 bits in it, , 5 bits unused. continue in manner.

the buffer not have 1 byte. 16-bit or 32-bit buffers common , more efficient. write out bytes whenever bits therein 8 or more, , shift remaining 0-7 bits start of buffer.

the tricky part @ end, since may have unused bits in last byte. huffman codes should have end symbol mark end of stream. know when should stop looking more huffman codes. if not have end code, need assure somehow either remaining bits in byte cannot complete huffman code, or need indicate in other way stream of bits end.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -