Im trying to read around 3000 binary files each binary file is written in little endian. I seek to the file offset and read the entire buffer into a qbytearray.
The binary format is int32 stringlength; (4 bytes) string stringname; + 'null'
File example:
//09 00 00 00 63 6f 6e 76 65 72 74 65 72 00 05 00 00 00 63 6f 75 6e 74 00
// 9 is the size then string.. then size then string..
QFile file("papers.bin");
if (!file.open(QIODevice::ReadOnly))
return;
file.seek(file.size() - 266 );
QByteArray binarydata = file.readall();
//09 00 00 00 63 6f 6e 76 65 72 74 65 72 00 05 00 00 00 63 6f 75 6e 74
//loop data
How would i loop the data inside the qbytearray?
Use a QDataStream
to read chunks:
QDataStream stream(binarydata);
stream.setByteOrder(QDataStream::LittleEndian);
while (!stream.atEnd()) {
qint32 length;
stream >> length;
QByteArray buf(length, 0);
stream.readRawData(buf.data(), length);
QString str = QString::fromUtf8(buf);
// do something with str
stream.skipRawData(1); // Skip the 00 byte
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.