在C ++中为Huffman编码逐位读写

时间:2014-10-18 16:37:47

标签: c++ encoding binary huffman-code

我试图用C ++对霍夫曼编码进行编码和解码。我不知道我的问题在哪里,我能够读写,但是当我解压缩文件时,它已经乱码,所以我没有正确编码或解码。 我认为它是在我写文件和阅读文件时出错的。所以这就是我必须编写的编码文件。首先,我将名为uMap的无序地图中的所有bitcode存储到一个字符串中:

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();

2 个答案:

答案 0 :(得分:0)

这些位正以与打包相反的顺序解压缩,可能是因为您对每个位使用不同的方法。第一个打包位进入位集的位0(位值&lt;&lt; 0)。解压缩的第一位来自第7位,因为setByte.to_string()在索引0处创建了第7位的字符串。问题被标记为霍夫曼编码但与之无关,而是与比特流操作有关。

答案 1 :(得分:0)

谢谢你的天气风向标,你读的是正确的,所以我照顾了它,但事实证明这也是我写作的方式。

新压缩:

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;
相关问题