Hello!

I'm having some problem with correctly creating and interpreting a struct through a QDataStream "operation". The goal here is the convert a struct into a QByteArray, send by socket to another application which will convert the raw data to the struct again. For now I'm using a Qt-based emulator which converts the raw data into a QByteArray before converting it to the struct.

The struct is the following:

Qt Code:
  1. struct re8k_ics_settings_inout
  2. {
  3. unsigned char header;
  4.  
  5. struct re8k_ics_settings_inout_nominalvoltages
  6. {
  7. unsigned int A;
  8. unsigned int B;
  9. unsigned int C;
  10. } nominalVoltages;
  11.  
  12. enum re8k_ice_nominal_frequency nominalFrequency;
  13. };
To copy to clipboard, switch view to plain text mode 

The enum have only two optional numbers (0 or 1). Here is the code containing both the serialiation and the deserialization:

Qt Code:
  1. //
  2. QByteArray serializedData;
  3. QDataStream dataStream(&serializedData,QIODevice::WriteOnly);
  4. dataStream.setVersion(QDataStream::Qt_4_8);
  5.  
  6. re8k_ics_settings_inout structTemp;
  7. structTemp.header = RE8K_ICDEF_SETTINGS_INOUT;
  8.  
  9. structTemp.nominalVoltages.A = 400;
  10. structTemp.nominalVoltages.B = 9999;
  11. structTemp.nominalVoltages.C = 1;
  12.  
  13. structTemp.nominalFrequency = Frequency50Hz; //0
  14.  
  15. dataStream << structTemp.header
  16. << quint32(structTemp.nominalVoltages.A) << quint32(structTemp.nominalVoltages.B) << quint32(structTemp.nominalVoltages.C)
  17. << structTemp.nominalFrequency;
  18.  
  19. emit signalSendToSystem(serializedData);
  20.  
  21. qDebug() << "Sended:" << serializedData.toHex() << serializedData.size() << (serializedData.size() == RE8K_ICDEF_SETTINGS_INOUT_SIZE) << RE8K_ICDEF_SETTINGS_INOUT_SIZE
  22. << structTemp.nominalVoltages.A << structTemp.nominalVoltages.B << structTemp.nominalVoltages.C;
  23.  
  24. //
  25. const QByteArray tempBA = serializedData.mid(0,RE8K_ICDEF_SETTINGS_INOUT_SIZE);
  26. const void* tempPointer = tempBA.constData();
  27. const re8k_ics_settings_inout* const newStruct = static_cast< const re8k_ics_settings_inout* >(tempPointer);
  28.  
  29. qDebug() << "Received:" << tempBA.toHex() << tempBA.size() << (tempBA.toHex() == serializedData.toHex()) << newStruct->nominalVoltages.A << newStruct->nominalVoltages.B << newStruct->nominalVoltages.C;
  30.  
  31. //
  32. re8k_ics_settings_inout newStruct2;
  33.  
  34. QDataStream deserialize(tempBA);
  35. deserialize.setVersion(QDataStream::Qt_4_8);
  36.  
  37. deserialize >> newStruct2.header
  38. >> newStruct2.nominalVoltages.A
  39. >> newStruct2.nominalVoltages.B
  40. >> newStruct2.nominalVoltages.C;
  41.  
  42. qDebug() << "Received 2:" << newStruct2.nominalVoltages.A << newStruct2.nominalVoltages.B << newStruct2.nominalVoltages.C;
To copy to clipboard, switch view to plain text mode 

And here is the result:

Qt Code:
  1. Sended: "0e000001900000270f0000000100000000" 17 false 20 400 9999 1
  2. Received: "0e000001900000270f0000000100000000" 17 true 654311568 15 1
  3. Received 2: 400 9999 1
To copy to clipboard, switch view to plain text mode 

So as you can see, convert the "received" QByteArray into the struct with QDataStream is actually working (400 9999 1), but if I try the way I'll have to use (using a reinterpret_cast or, in this case, some static_cast for more safety), I get some bizarre errors. Not only this, but the RE8K_ICDEF_SETTINGS_INOUT_SIZE (which is sizeof(re8k_ics_settings_inout)), returns 20 while the QByteArray produced by the QDataStream has a size of 17. So my interpretation here is that is the QDataStream that is doing the mess. And to add a extra comment, the system works fine if the biggest number I put in nominalVoltages substruct is 255 (but still with the different size problem).


How may I solve this problem?

Thanks,

Momergil