霍夫曼算法汇编
我需要编写一个程序来使用
我编写的霍夫曼算法来压缩/解压缩txt文件,它对于字符数少于缓冲区大小的文件很有用,但对于字符数较多的文件则不起作用。
我的问题是将压缩缓冲区与解压缩缓冲区连接起来。
因此,如果压缩写入的字节数(包含要遍历树的 1 和 0)与解压缩读取的字节数不同,则它不起作用。 例如,如果压缩缓冲区写入200,我需要解压缩缓冲区正好读取200字节。
如果我将解压缩的大小设置为读取 200,则压缩会在某个地方写入 200,其他时候会写入小于或大于 200。
您能否建议如何跟踪每次压缩写入的字节数并将其传输到解压缩部分?
I need to write a program to compress/decompress txt files using Huffman algotrithm
I have writen it, and it works good for files that have less charachters than the buffer size, but it doesnt work for files with greater number of characters.
My problem is to interface compression buffer with decompression buffer.
So if the number of bytes written by the compression (which contains the 1 and 0 to go through the tree), is different from the number of bytes the decompression reads it does not work.
Example, if the buffer of the compression writes 200, I need the buffer of decompression to read exactly 200 bytes.
If i set the size of decompression to read 200, somewhere the compression will write 200 and other times less or more than 200.
Can you suggest anything how to keep track of the numbers of byte written by compression each time and transmit it to decompression part?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“跟踪”流末尾的常见方法是专门为此用途添加 N+1“EOF”符号。这样,您就不需要维护任何“大小”计数器。
A common way to "track" the end of stream is to add an N+1 "EOF" symbol specifically for this usage. This way, you don't need to maintain any "size" counter.
我没有使用任何缓冲区。在我的文件的标题中,我写下了代码长度和代码本身。因此,当我想解压缩文件时,首先我从标头中读取代码长度和代码(您也可以在标头中放入几个字节来检查文件的正确性:例如 XXY,因此如果文件不是以这些字节开头,则它已损坏)。在读取代码长度和代码后,是时候解码其余数据了。您可以通过以下方式对其进行解码:
I did't use any buffers. In header of my file I write down code length, and code itself. So when I want to decompress my file, first I read code lengths and codes from my header (you can also put few bytes in header to check correctness of file: for example XXY, so if file does not start with these bytes, its corrupted). After I read my code lengths, and my codes, it is time to decode rest of data. You can decode it in this way: