C++编码中逐位读写赫夫曼编码 我试图在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); }

C++编码中逐位读写赫夫曼编码 我试图在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); },c++,encoding,binary,huffman-code,C++,Encoding,Binary,Huffman Code,然后在减压侧: 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();

然后在减压侧:

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

位的解包顺序与打包顺序相反,可能是因为您对每个位使用不同的方法。第一个压缩位进入位集bitvalue的第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;
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;

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