Results 1 to 6 of 6

Thread: QDataStream and serialization

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QDataStream and serialization

    Ok ... maybe it's me who's got really tired but:

    I have this struct:
    Qt Code:
    1. #pragma pack(1)
    2. typedef struct {
    3. quint8 DLE; // ASCII DLE character (16 decimal)
    4. quint8 packet_id;
    5. quint8 size_app_payload; // number of bytes of packet data (bytes 3 to n-4)
    6. char app_payload[255]; // 0 to 255 bytes
    7. quint8 checksum; // 2's complement of the sum of all bytes from byte 1 to byte n-4 (end of the payload)
    8. quint8 DLE_end; // same as DLE
    9. quint8 ETX; // End of text - ASCII ETX character (3 decimal)
    10. } serial_packet_format;
    11. #pragma pack()
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. QByteArray byteArray;
    2. QDataStream stream(&byteArray, QIODevice::WriteOnly);
    3.  
    4. stream<< sPacket.DLE // DLE
    5. << sPacket.packet_id // packet id
    6. << sPacket.size_app_payload; // app_payload size
    To copy to clipboard, switch view to plain text mode 

    and so on for the rest...

    now when I print the resulting byte array
    Qt Code:
    1. for (i = 0; i < byteArray.size(); i++) {
    2. printf("%02X\r\n", (qint8) byteArray[i] );
    3. }
    To copy to clipboard, switch view to plain text mode 

    this is what I get!

    10
    FFFFFFA1
    02

    Why 0xA1 turned into 0xFFFFFFA1 ?!

    Could someone please take me out of my misery?

    Best regards,
    Pedro Doria Meunier
    Last edited by pdoria; 10th November 2009 at 18:03. Reason: updated contents

Similar Threads

  1. XML Serialization of Qt Objects
    By sasi in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2009, 19:25
  2. QDataStream question...
    By grellsworth in forum Qt Programming
    Replies: 10
    Last Post: 21st April 2008, 12:17
  3. QTcpSocket and QDataStream
    By December in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2008, 14:13
  4. Serialization
    By donmorr in forum Qt Programming
    Replies: 4
    Last Post: 16th November 2006, 13:51
  5. [Qt4] qdatastream/qstring serialization
    By fabo in forum Qt Programming
    Replies: 4
    Last Post: 19th April 2006, 18:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.