[英]Reading and writing bit by bit in C++ for Huffman Encoding
我正在尝试对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();
这些位以与打包相反的顺序解包,这可能是因为您对每个位使用了不同的方法。 打包的第一位进入位集的位0(位值<< 0)。 解压缩的第一位来自第7位,因为setByte.to_string()
创建了一个索引为0的第7位字符串。该问题被标记为霍夫曼编码,但与之无关,与比特流操作有关。
谢谢您,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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.