Results 1 to 6 of 6

Thread: QDataStream to QByteArray and ViceVersa

  1. #1

    Default QDataStream to QByteArray and ViceVersa

    Qt Code:
    1. QByteArray data = QByteArray::fromHex("200000008305370FFFFFF630500008E");
    2. QDataStream dataStreamToReadFrom(&data, QIODevice::ReadOnly);
    3. QByteArray convertedData;
    4. dataStreamToReadFrom >> convertedData;
    5.  
    6. QString Str = data.toHex();
    7. QString convertedStr = convertedData.toHex();
    To copy to clipboard, switch view to plain text mode 

    Here Str and convertedStr are not same. Why?

    I want to bothe QByteArray data and converted Data to be the same. How to achieve that.?
    Last edited by d_stranz; 24th March 2021 at 20:12. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    530
    Thanked 81 Times in 76 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream to QByteArray and ViceVersa

    What do you try to achieve? Why do you think that both should be the same? QDataStream has an internal stream format so it adds some bits and bytes to your data so it knows what kind of data it is.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,708
    Thanks
    13
    Thanked 1,603 Times in 1,530 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDataStream to QByteArray and ViceVersa

    QDataStream is, in general, expecting to be reading a data stream created by another QDataStream. If just want to read raw bytes then use QIODevice directly.

    The QDataStream output of a QByteArray contains bytes other than just the content of the array. Consequently, when asked to read a QByteArray it expects those extra bytes. QByteArray serialises the size of the array as a 32-bit integer before the content itself so the receiver can know where the data ends.
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDataStream>
    3. #include <QBuffer>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QCoreApplication app(argc, argv);
    9.  
    10. // Load a test QByteArray with 8 bytes
    11. QByteArray payload = QByteArray::fromHex("3031323334353637");
    12.  
    13. // Serialise it using QDataStream
    14. QBuffer buffer;
    15. buffer.open(QIODevice::WriteOnly);
    16. QDataStream out(&buffer);
    17. out << payload;
    18.  
    19. // What went into the buffer?
    20. qDebug() << buffer.buffer().toHex();
    21.  
    22. // Output: "000000083031323334353637"
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 


    In your example the input hex string is 31 digits (odd) and QByteArray has assumed a leading zero. The data array contains bytes (hex): 02 00 00 00 08 30 53 70 ff ff ff 63 05 00 00 8e
    The first four bytes will be consumed as a size (a very big one), and the content will start at 0x08.
    On my machine convertedStr is empty, probably because the input stream was exhausted before 0x02000000 bytes could be read.

  4. #4

    Default Re: QDataStream to QByteArray and ViceVersa

    Quote Originally Posted by ChrisW67 View Post
    QDataStream is, in general, expecting to be reading a data stream created by another QDataStream. If just want to read raw bytes then use QIODevice directly.

    The QDataStream output of a QByteArray contains bytes other than just the content of the array. Consequently, when asked to read a QByteArray it expects those extra bytes. QByteArray serialises the size of the array as a 32-bit integer before the content itself so the receiver can know where the data ends.
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDataStream>
    3. #include <QBuffer>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QCoreApplication app(argc, argv);
    9.  
    10. // Load a test QByteArray with 8 bytes
    11. QByteArray payload = QByteArray::fromHex("3031323334353637");
    12.  
    13. // Serialise it using QDataStream
    14. QBuffer buffer;
    15. buffer.open(QIODevice::WriteOnly);
    16. QDataStream out(&buffer);
    17. out << payload;
    18.  
    19. // What went into the buffer?
    20. qDebug() << buffer.buffer().toHex();
    21.  
    22. // Output: "000000083031323334353637"
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 


    In your example the input hex string is 31 digits (odd) and QByteArray has assumed a leading zero. The data array contains bytes (hex): 02 00 00 00 08 30 53 70 ff ff ff 63 05 00 00 8e
    The first four bytes will be consumed as a size (a very big one), and the content will start at 0x08.
    On my machine convertedStr is empty, probably because the input stream was exhausted before 0x02000000 bytes could be read.
    Thanks, ChrisW67 for a detailed explanation, but I have a different use case.

    In my code, I have to pass a QByteArray to a generic API which is taking QDataStream as a parameter. In that API I need to again deserialize the QDataStream to QByteArray.

    Something like
    Qt Code:
    1. void readContainer(QDataStream* stream)
    2. {
    3. //Here I need to read QByteArray from QDataStream
    4. QByteArray convertedData;
    5. stream>> convertedData;
    6. //print converted data.
    7. //But the data is coming as empty for me
    8. }
    9.  
    10. int main()
    11. {
    12. QByteArray data = QByteArray::fromHex("200000008305370FFFFFF630500008E");
    13. QDataStream dataStreamToReadFrom(&data, QIODevice::ReadOnly);
    14. readContainer(dataStreamToReadFrom);
    15. return 0;
    16. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    530
    Thanked 81 Times in 76 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream to QByteArray and ViceVersa

    Then you should create the data via QDataStream. You can't simply throw some random bytes to QDataStream and hope it's properly decoded afterwards.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,708
    Thanks
    13
    Thanked 1,603 Times in 1,530 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDataStream to QByteArray and ViceVersa

    Quote Originally Posted by alokraj001 View Post
    Thanks, ChrisW67 for a detailed explanation, but I have a different use case.

    In my code, I have to pass a QByteArray to a generic API which is taking QDataStream as a parameter. In that API I need to again deserialize the QDataStream to QByteArray.
    A QDataStream object is not the data, nor does it contain the data, it merely decodes a set of bytes provided to it. You need to construct a correct set of bytes to provide it.

    Assuming you are expecting the "other end" to receive exactly the bytes you have in your code:
    Qt Code:
    1. QByteArray payload = QByteArray::fromHex("0200000008305370FFFFFF630500008E");
    2. // Serialise it using QDataStream
    3. QBuffer buffer;
    4. buffer.open(QIODevice::WriteOnly);
    5. QDataStream out(&buffer);
    6. out << payload;
    7. buffer.close();
    To copy to clipboard, switch view to plain text mode 
    Then buffer contains the bytes you need to transmit, and
    Qt Code:
    1. if (buffer.open(QIODevice::ReadOnly)) {
    2. QDataStream passThis(&buffer);
    3. readContainer(passThis);
    4. }
    To copy to clipboard, switch view to plain text mode 
    should get the bytes there.

Similar Threads

  1. Help with QDataStream and QByteArray
    By P@u1 in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2011, 00:25
  2. QByteArray,QDataStream Question
    By aash_89 in forum Qt Programming
    Replies: 3
    Last Post: 21st July 2010, 21:40
  3. QTcpSocket QDataStream QByteArray
    By Grimlock in forum Newbie
    Replies: 1
    Last Post: 14th December 2009, 22:47
  4. Replies: 9
    Last Post: 25th July 2009, 13:27
  5. how QDataStream and QByteArray related
    By dognzhe in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2009, 08:45

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.