Reputation: 2818
Help please with these conversions. This is input;
QByteArray byteArr;
QBitArray bitArr;
QFile file(adress);
byteArr.readAll(file);
Convertion from bytes to bits:
BitArr.resize(8*byteArr.count());
for(int i=0; i<byteArr.count(); ++i)
for(int b=0; b<8; ++b)
bitArr.setBit(i*8+b, byteArr.at(i)&(1<<b));
Convertion from bits to bytes:
for(int i=0; i<bitArr.count(); ++i)
byteArr[i/8] = (byteArr.at(i/8) | ((bitArr[i]?1:0)<<(i%8)));
And showing the result
ui->textBrowser->setText(QString(byteArr));
This code works fine perfect with ASCII letters. But if I opened Unicode or UTf-8 of ANSI text, I have runtime error or wrong output. Please help
Upvotes: 1
Views: 1946
Reputation: 40512
Default QString
constructor uses Latin1 encoding. Use QString::fromUtf8(byteArr)
instead of QString(byteArr)
. Conversion between QByteArray and QBitArray is not related to this issue.
Upvotes: 2