【问题标题】:Reading and writing bit by bit in C++ for Huffman Encoding在 C++ 中逐位读写霍夫曼编码
【发布时间】:2014-10-18 16:37:46
【问题描述】:

我正在尝试对 C++ 中的霍夫曼编码进行编码和解码。我不确定我的问题出在哪里,我可以读写,但是当我解压缩文件时,它被打乱了,所以我要么编码不正确,要么解码不正确。 我认为当我在编写和读取文件时出现问题。所以这就是我必须编写的编码文件。首先,我将名为 uMap 的无序地图中的所有位码存储到一个字符串中:

int i = 0, j = 0;
string fullStr = "";
for (i = 0; i < buffsize; i++) //put all codes in one string of 1's and 0's
    fullStr += uMap[buffer[i]];
unsigned char byte = 0;
i = 0;
for (j = 0; j < fullStr.length(); j++)
{

    if (i != 8)
    {
        byte |= (fullStr[j] == '1') << i; // make up one byte
        i++;
    }
    else
    {
        outf.put(byte); // write one byte at a time
        byte = 0;
        i = 0;
    }
}
if (i != 0 && i < 8)
{
    while (i<8)
    {
        byte |= 0 << i; // finish up last byte if not finished
        i++;
    }
    outf.put(byte);
}

然后在解压端:

int i = 0;
unsigned char byte = 0;
bitset<8> setByte;
ofstream outf(filename, ofstream::binary);
string concat = "";
string bitStr = "";
for (i = 0; i < buffLength; i++)
{
    setByte = buffer[i];
    bitStr = setByte.to_string();
    for (int j = 0; j < 8; j++)
    {
        concat += bitStr[j];
        if (uMap[concat])
        {
            //cout << "found code " << concat << " " << uMap[concat] << endl;
            outf.put(uMap[concat]);
            concat = "";
        }
    }
}
outf.close();

【问题讨论】:

  • 这是使用调试器的主要示例。笔和纸也可能有帮助。对于简单的代码审查来说,这看起来太复杂了。

标签: c++ encoding binary huffman-code


【解决方案1】:

这些位的解包顺序与打包相反,可能是因为您对每个位使用了不同的方法。打包的第一位将进入位集的位 0(位值 setByte.to_string() 在索引 0 处创建了第 7 位的字符串。该问题被标记为 Huffman 编码,但与它无关,它与位流操作有关。

【讨论】:

    【解决方案2】:

    谢谢你,Weather Vane,你对他们被反转的阅读是正确的,所以我处理了这个,但事实证明这也是我写它的方式。

    新压缩:

    int i = 0, j = 0;
    string fullStr = "";
    for (i = 0; i < buffsize; i++) //put all codes in one string
        fullStr += uMap[buffer[i]];
    for (i = 0; i < fullStr.length(); i+=8)
    {
        unsigned char byte = 0;
        string str8 = "";
        if (i + 8 < fullStr.length())
            str8 = fullStr.substr(i, i + 8);
        else
            str8 = fullStr.substr(i, fullStr.length());
        for (unsigned b = 0; b != 8; ++b)
        {
            if (b < str8.length())
                byte |= (str8[b] & 1) << b; // this line was wrong before
            else
                byte |= 1 << b;
        }
        outf.put(byte);
    }
    int filelen = outf.tellp();
    outf.close();
    

    新解压:

    int i = 0,j=0,k=0;
    unsigned char byte = 0;
    bitset<8> setByte, reverseByte;
    ofstream outf(filename, ofstream::binary);
    string concat = "";
    string bitStr = "";
    string reverse = "";
    int charCount = 0;
    for (i = 0; i < buffLength; i++)
    {
        setByte = buffer[i];
        bitStr = setByte.to_string();
        reverse = "";
        for (k = 7; k>=0; k--)
            reverse += bitStr[k];
        for (j = 0; j < 8; j++)
        {
            concat += reverse[j];
            if (uMap[concat])
            {
                outf << uMap[concat];
                charCount++;
                concat = "";
                if (charCount == origLength) // if we have written original amount stop
                {
                    outf.close();
                    return 1;
                }
    
            }
        }
    }
    outf.close();
    return 1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多