Qt:<bool> QVector to QByteArray
Qt: QVector<bool> to QByteArray
你好,请帮我转换。布尔到字节数组的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: 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()返回的char被隐式转换为QChar-对于值>127,这可能是一个问题,查找错误的值,这取决于查找是如何创建的。=>更好地使用QMap甚至QVector,其中256个条目具有char作为索引,这要快得多。
如果键不存在,则表[…]将向映射插入一个新的空条目。我不认为这是这里的意图-最好使用table.value().
我也不记得了,但我认为huffman存储的比特从比特0开始,然后填充到最高有效比特,所以也许
buf = buf | x[n] << (7 - count);
应该反过来吗?
祝你好运。
相关文章:
- "error: no matching function for call to"构造函数错误
- 将 Qvector<uint8_t> 转换为 QString
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 正确清洁 QVector
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- QDataStream read to QVector
- Qt:<bool> QVector to QByteArray