Results 1 to 6 of 6

Thread: QDataStream and serialization

  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 19:03. Reason: updated contents

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

    Default Re: QDataStream and serialization

    :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.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataStream and serialization

    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:
    Qt Code:
    1. for (i = 0; i < byteArray.size(); i++) {
    2. printf("%02X\r\n", (quint8) byteArray[i] );
    3. }
    To copy to clipboard, switch view to plain text mode 

    Note the quint8 instead of qint8

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

    Default Re: QDataStream and serialization

    Done that my friend.

    data still gets 0xFFFFFFA1 in the stream ...

    Thanks.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataStream and serialization

    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream and serialization

    What do you get if you read the data back from the stream?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

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