0

I have the following region of data being read into a QDataStream object :

DE 07 05 19 0E 28 1A 

This should translate to the date/time: 25-05-2014 15:40:26

I am trying to use the following to read this into a QDateTime variable:

QFile fileIn(iFile);
if (!fileIn.open(QIODevice::ReadOnly)) return;
QDataStream data(&fileIn);
data.setByteOrder(QDataStream::LittleEndian);
data.setVersion(QDataStream::Qt_5_0);
data.skipRawData(32);

.
.
QDateTime time;
data >> time;
qDebug () << time.date();

Instead I get null/blank in time. Output is:

QDate("")
TenG
  • 3,843
  • 2
  • 25
  • 42
  • How was the data created? Can you give a minimal example that serializes the date, and then deserializes it? – Frank Osterfeld Dec 25 '15 at 21:10
  • I can't give an example of how the data was serialized as I am reverse engineering this data. I will amend the question with more of my code. – TenG Dec 25 '15 at 21:16
  • Was the data serialized using QDataStream/its protocol at all? If not, it makes no sense to use QDataStream. – Frank Osterfeld Dec 25 '15 at 21:22
  • I assume so. I am able to extract other data for qString, qint8, quint16, even QList OK. – TenG Dec 25 '15 at 21:24
  • Have you tried to serialize `25-05-2014 15:40:26` from Qt code? Do you acually end up with `DE 07 05 19 0E 28 1A`? If not, no reason it will work the way around... – jpo38 Dec 26 '15 at 07:56
  • @jpo38. No I haven't tried that. I should clarify I don't expect the data formatting to be present/the same, but I think QDateTime should take care of that. I doubt this is an environment issue since the data files I am processing are multi-platform. – TenG Dec 26 '15 at 11:02
  • 1
    But how did you get `DE 07 05 19 0E 28 1A` in the first place? – peppe Dec 26 '15 at 11:17
  • @peppe, the data is in files from an application for which I am investigating whether an import feature can be added to our system to support it. Based on other regions in the file I am able to use direct read into quint8, qint16, qint32 and qstring variables from the file/QDataStream, so I assume I just need to find an appropriate type. Which is why I tried QDateTime. I can of course just each bit e.g. qint16 yyyy = getInt16(data), etc, then join it all together, so I do have a workaround. – TenG Dec 26 '15 at 15:30
  • 2
    The fact is that serialization through `QDataStream` of a `qint16` is "just" writing its contents as two bytes (in a given endianess). That's pretty much compatible with anything else that uses the same. But the serialization of a `QDateTime` is [way more complicated](https://doc.qt.io/qt-5/datastreamformat.html) and very specific to Qt. Unless you produced the bytes with the same semantics, you can't read them back with `QDataStream` (how could Qt know the meaning of the bytes in the stream?). Deserialize "properly" given the writer semantics, and then reassemble a `QDateTime`. – peppe Dec 26 '15 at 16:52
  • OK. It seems that I should stick with the method I have used for this type of data, i.e. read the yyyy, mm, dd, hh, mi, ss separately. Thanks all for your responses . – TenG Dec 29 '15 at 17:30

1 Answers1

0

The first thing here is how on earth this data is serialised. I've had a look and can't work it out: each byte doesn't nicely convert to each part of date/time, the bytes you are trying to reverse engineer aren't the epoch value of that date converted into hex. You can't expect QDateTime to magically know your raw data format. Report back with data format and I'll try and help.

SionHughes
  • 128
  • 11
  • As with the other responses, it seems that I should stick with the method I have used for this type of data, i.e. read the yyyy, mm, dd, hh, mi, ss separately. Thank you for your help. – TenG Dec 29 '15 at 17:31