Forgive me if "parse" only applies to text-based data, but the idea is the same. I have a chunk of bytes stored in a 30-byte QByteArray that I extracted from a very organized external file via QFile::read(int). I know that the first 12 bytes is 3 longs, the next 2 bytes is an unsigned short, etc.
QFile infile
("c:\temp\file.bin");
//... open the file, etc etc
QFile infile("c:\temp\file.bin");
//... open the file, etc etc
QByteArray chunk = infile.read(30);
To copy to clipboard, switch view to plain text mode
I then pass that QByteArray into a parser function running in another thread while the main thread moves onto the next chunk. This happens millions of times in sequence, so wrapping that QByteArray into a QDataStream as suggested in the documentation slows the process down so much that I want to try manipulating the QByteArray directly.
How do I (for example) read the first 4 bytes from a QByteArray, concatenate them, then cast the result into a long int? I'm looking for a faster equivalent to this that doesn't require me to construct a QDataStream (and, by extension, a QBuffer):
qint32 data;
io >> data;
QDataStream io(chunk);
qint32 data;
io >> data;
To copy to clipboard, switch view to plain text mode
I've tried this:
bool ok = true;
chunk.mid(0,4).toInt(&ok); //ok==false
bool ok = true;
chunk.mid(0,4).toInt(&ok); //ok==false
To copy to clipboard, switch view to plain text mode
but it doesn't work since the data is binary, not characters.
The only option I've come up with looks really ugly:
quint32 a = chunk.at(0);
quint32 b = chunk.at(1);
quint32 c = chunk.at(2);
quint32 d = chunk.at(3);
quint32 abcd = 0 | (a << 24) | (b << 16) | (c << 8) | d;
quint32 a = chunk.at(0);
quint32 b = chunk.at(1);
quint32 c = chunk.at(2);
quint32 d = chunk.at(3);
quint32 abcd = 0 | (a << 24) | (b << 16) | (c << 8) | d;
To copy to clipboard, switch view to plain text mode
I'm not even sure that this works or if it's faster! Of course, I still have to deal with endianness and all that, but let's just assume that's not a problem for now.
Any suggestions?
Bookmarks