C++ Qt:QVector到QByteArray
你好,请帮我兑换。bool到字节数组的QVectorC++ Qt:QVector到QByteArray,c++,algorithm,qt,bytearray,bitarray,C++,Algorithm,Qt,Bytearray,Bitarray,你好,请帮我兑换。bool到字节数组的QVector QByteArray currentArray //get(currentArray); currentArray is just text. QMap <QChar, QVector<bool> > table; //creating table; //table: is set of QChar and bit code //0: 100110111001 //1: 00011 //2: 011110 //3: 0
QByteArray currentArray
//get(currentArray); currentArray is just text.
QMap <QChar, QVector<bool> > table;
//creating table;
//table: is set of QChar and bit code
//0: 100110111001
//1: 00011
//2: 011110
//3: 010001
//...
QByteArray compressedArray;
//converting QVector<bool> from QMap to QByteArray
//it do not work fine.
int count=0;
Сhar buf=0;
i=0;
while(i<currentArray.size())
{
QVector <bool> x = table[currentArray.at(i++)];
for(int n=0; n < x.size(); n++)
{
buf = buf | x[n] << (7 - count);
count ++;
if (count == 8)
{
count=0;
compressedArray += buf;
buf = 0;
}
}
}
这是哈夫曼算法的一个实现。
解密工作正常,所以问题就在这里。我不能说问题的确切原因是什么,但代码中有几个问题可能会导致问题:
QVector <bool> x = table[currentArray.at(i++)];
currentArray.at返回的字符隐式转换为QChar-对于值>127,这可能是一个问题,查找错误的值,这取决于查找是如何创建的。=>最好使用QMap甚至QVector,256个条目都有char作为索引,这会更快
表[…]将在没有密钥的情况下向映射插入一个新的空条目。我不认为这是有意的-更好地使用table.value
我也不记得了,但我认为哈夫曼存储的位从0位开始,然后填充到最重要的位,所以可能
buf = buf | x[n] << (7 - count);
应该反过来吗
祝你好运。@Abhishek Bansal经过反向运算解码后,我得到了错误的文件。我确信解码是正确的。什么时候调试应用程序?可能是因为您希望x[0]是最低有效位,而不是像这里一样的最高有效位