PDA

View Full Version : Streaming bits to a QByteArray using QDataStream



Miss Engineer
28th August 2014, 03:28
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.

QByteArray dataset=new QByteArray();
QDataStream outtopacket =new QDataStream(dataset, QIODevice::WriteOnly);


Now if I stream a bit:


bool mode = false;
*outtopacket<<mode;

and then stream a byte

quint8 data = 15;
*outtopacket<<data;

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?

wysota
28th August 2014, 07:16
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.

Miss Engineer
28th August 2014, 10:55
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.

anda_skoa
28th August 2014, 11:47
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,
_

Miss Engineer
28th August 2014, 14:45
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..

wysota
28th August 2014, 14:54
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.

ChrisW67
28th August 2014, 21:26
Are you sure that the 32 extra bits are not packed together either before or after the 32 7-byte blocks?

Miss Engineer
3rd September 2014, 07:46
I ended up using QBitArray.. it wasn't straight forward but it worked.