PDA

View Full Version : Reading string in Utf8 from binary file



iddqd
17th July 2010, 13:14
Hi,
I'm trying to read a string in Utf8 encoding from binary file through using QDataStream.

Binary file has some records like this:

06000000D098D0BCD18F
where
06000000 - quint32, text data size in bytes,
D098D0BCD18F - text in utf8, just a 3 chars in russian

I'm trying to do like this:

QDataStream in(&file);
...
quint32 labelLen;
in >> labelLen;
qDebug() << "labelLen=" << labelLen;

char * label = new char [labelLen];
in.readRawData(label, labelSize);
qDebug() << "label=" << label;

But as a result in console I'm getting some unreadable data, not a text...

How to read a data correctly and convert readed a text data to QString?

tbscope
17th July 2010, 13:44
Using readRawData is wrong.

Quoting the documentation:

The data is not encoded.

This means that the text is not read as UTF8, instead, the character "D0 98" is not read as one character but as two distinct characters. This is why you don't see Cyrillic text.

To overcome this, I advise to use a QTextStream to read the UTF encoded data in the file.

ChrisW67
18th July 2010, 23:15
Check your byte order. QDataStream is designed to read streams written by QDataStream. This code:

quint32 labelLen = 6;

QByteArray arr;
QBuffer buf(&arr);
buf.open(QIODevice::WriteOnly);
QDataStream s(&buf);
s << labelLen;
buf.close();

qDebug() << arr.toHex();

outputs "00000006" not "06000000". Your labelLen figure is 100663296 rather than 6.

iddqd
19th July 2010, 06:16
Thanks for answer. Byte order is ok, labelLen=6. I've already solved a problem by using

qDebug() << "label=" << QString::fromUtf8(label, labelLen);