PDA

View Full Version : QDataStream and serialization



pdoria
10th November 2009, 17:17
Ok ... maybe it's me who's got really tired :) but:

I have this struct:


#pragma pack(1)
typedef struct {
quint8 DLE; // ASCII DLE character (16 decimal)
quint8 packet_id;
quint8 size_app_payload; // number of bytes of packet data (bytes 3 to n-4)
char app_payload[255]; // 0 to 255 bytes
quint8 checksum; // 2's complement of the sum of all bytes from byte 1 to byte n-4 (end of the payload)
quint8 DLE_end; // same as DLE
quint8 ETX; // End of text - ASCII ETX character (3 decimal)
} serial_packet_format;
#pragma pack()


filled it with:
sPacket Contents
DLE: 10
Packet id: A1
size app payload: 02
Payload:
01
00
Checksum: 5C
DLE end: 10
ETX: 03

Now the serialization:

QByteArray byteArray;
QDataStream stream(&byteArray, QIODevice::WriteOnly);

stream<< sPacket.DLE // DLE
<< sPacket.packet_id // packet id
<< sPacket.size_app_payload; // app_payload size


and so on for the rest...

now when I print the resulting byte array

for (i = 0; i < byteArray.size(); i++) {
printf("%02X\r\n", (qint8) byteArray[i] );
}

this is what I get!

10
FFFFFFA1
02

Why 0xA1 turned into 0xFFFFFFA1 ?! :confused:

Could someone please take me out of my misery? :)

Best regards,
Pedro Doria Meunier

pdoria
10th November 2009, 20:33
:bump:

Guys I know this to be one the dumbest threads ever...
However... I'm baffled by it and it's interfering in sending data.

Any pointers highly appreciated.

BR,
Pedro.

squidge
10th November 2009, 20:57
You asking printf to treat an integer as a qint8, so what do you expect? Anything more than 0x7F will always be negative number.

Maybe this is what you meant:


for (i = 0; i < byteArray.size(); i++) {
printf("%02X\r\n", (quint8) byteArray[i] );
}


Note the quint8 instead of qint8 :)

pdoria
10th November 2009, 20:59
Done that my friend.

data still gets 0xFFFFFFA1 in the stream ...

Thanks.

squidge
10th November 2009, 23:47
Actually in the stream? Or just in the output?

Your putting 3 bytes into a QByteArray, so it can not be putting 0xFFFFFFA1 in the stream. Have you tried copying the QByteArray to a standard quint8 array and viewing the memory in the debugger?

wysota
11th November 2009, 09:42
What do you get if you read the data back from the stream?