Results 1 to 8 of 8

Thread: Streaming bits to a QByteArray using QDataStream

  1. #1
    Join Date
    Apr 2014
    Posts
    14
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Streaming bits to a QByteArray using QDataStream

    Hello,
    I want to create a packet of data from a collection of bits and bytes and later store in a binary file.. I am using QDataStream as I figured it is the only way to serialise my data.
    Qt Code:
    1. QByteArray dataset=new QByteArray();
    2. QDataStream outtopacket =new QDataStream(dataset, QIODevice::WriteOnly);
    To copy to clipboard, switch view to plain text mode 

    Now if I stream a bit:
    Qt Code:
    1. bool mode = false;
    2. *outtopacket<<mode;
    To copy to clipboard, switch view to plain text mode 
    and then stream a byte
    Qt Code:
    1. quint8 data = 15;
    2. *outtopacket<<data;
    To copy to clipboard, switch view to plain text mode 

    how is it going to deal with the bit? Is it going to pad it with zeros? Will the next data streamed into the packet occupy the next empty bit in QByteArray?
    Any better way to serialize my data?

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

    Default Re: Streaming bits to a QByteArray using QDataStream

    Why do you think "mode" is a bit?

    QDataStream serializes a bool as an eight bit integer (0 vs 1, most likely). quin8 is also serialized as an eight bit integer (unsigned). So in this particular case the output will contain two bytes - 0x00 0x0F.
    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.


  3. The following user says thank you to wysota for this useful post:

    Miss Engineer (28th August 2014)

  4. #3
    Join Date
    Apr 2014
    Posts
    14
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Streaming bits to a QByteArray using QDataStream

    Quote Originally Posted by wysota View Post
    Why do you think "mode" is a bit?

    QDataStream serializes a bool as an eight bit integer (0 vs 1, most likely). quin8 is also serialized as an eight bit integer (unsigned). So in this particular case the output will contain two bytes - 0x00 0x0F.
    Oh I see.. Suggestions on how I can create my packet would be highly appreciated.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Streaming bits to a QByteArray using QDataStream

    Any specific reasons you want the bits to be so compressed?

    You'd basically end up with lots of shifting and masking operations that you would avoid by simply using the same size that the data occupies in memory.

    Cheers,
    _

  6. #5
    Join Date
    Apr 2014
    Posts
    14
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Streaming bits to a QByteArray using QDataStream

    I have a packet structure that I have to adhere to.. the packet consists of a 4 byte time field and then 32 datasets, each of which have a size of 57 bits. the first bit indicates the mode, while the other 56 bits consist of 8 bit fields..

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

    Default Re: Streaming bits to a QByteArray using QDataStream

    And why do you want to fill the structure using QDataStream? This doesn't make sense. You don't want the data serialized, you want it packed into a specific data structure. There are bit shifting operators in C++, use them.
    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.


  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Streaming bits to a QByteArray using QDataStream

    Are you sure that the 32 extra bits are not packed together either before or after the 32 7-byte blocks?

  9. #8
    Join Date
    Apr 2014
    Posts
    14
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Streaming bits to a QByteArray using QDataStream

    I ended up using QBitArray.. it wasn't straight forward but it worked.

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. Replies: 9
    Last Post: 25th July 2009, 13:27
  3. Streaming QImage (QByteArray, QDataStream, QBuffer)
    By knarz in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2009, 22:05
  4. QVariant streaming from QDataStream
    By XCG in forum Newbie
    Replies: 3
    Last Post: 26th February 2008, 17:12
  5. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23

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
  •  
Qt is a trademark of The Qt Company.