punksta
punksta

Reputation: 2818

QByteArray to QBitArray to QbyteArray to QString + Unicode characters

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

Answers (1)

Pavel Strakhov
Pavel Strakhov

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

Related Questions