Convert a QByteArray in Hex to a Struct
-
@SGaist I actually don't know. I am supposed to read only, another program (I don't have access to) does the writting.
-
Would it be possible for you to request to know how it's done ?
If the writing is done using QDataStream it will simplify things.
-
Would it be possible for you to request to know how it's done ?
If the writing is done using QDataStream it will simplify things.
-
QDataStream in(fileData); struct header; in >> header;
You should check whether the QDataStream used has its version set. If not, then you should suggest to add that so that you can ensure that if a future version of Qt changes some aspect of the QDataStream protocol you can still read the old files you have generated.
-
QDataStream in(fileData); struct header; in >> header;
You should check whether the QDataStream used has its version set. If not, then you should suggest to add that so that you can ensure that if a future version of Qt changes some aspect of the QDataStream protocol you can still read the old files you have generated.
-
if the writing was done like this:
QDataStream out(file); out << myStruct:
then there's nothing special to do.
-
if the writing was done like this:
QDataStream out(file); out << myStruct:
then there's nothing special to do.
-
I knew I was forgetting something, you have to implement the QDataStream operator for the structure.
Which is something that has likely be done for the other application. The operators implementation boils down to sending each field to the output stream and read each field from the input stream.
-
@SGaist said in Convert a QByteArray in Hex to a Struct:
The operators implementation boils down to sending each field to the output stream and read each field from the input stream.
I was wondering about that. I read a bunch of articles last week about transferring binary data. Two of the major concerns are binary alignment/packing and endianess. Does the QDataStream address both of those issues? I am more concerned with the alignment/packing as all our systems are little endian. The alignment/packing can change if compiler settings are changed.
Edit:
Sorry, I should have just looked it up:
http://doc.qt.io/qt-5/qdatastream.html -
I knew I was forgetting something, you have to implement the QDataStream operator for the structure.
Which is something that has likely be done for the other application. The operators implementation boils down to sending each field to the output stream and read each field from the input stream.
-
Yes it is, but you have to ensure the the writing was done in the same order.
-
You're welcome !
Qt is C++ so by learning the second you'll be able to master the first :)