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.
Now if I stream a bit:
Code:
bool mode = false;
*outtopacket<<mode;
and then stream a byte
Code:
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?
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.
Re: Streaming bits to a QByteArray using QDataStream
Quote:
Originally Posted by
wysota
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.
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,
_
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..
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.
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?
Re: Streaming bits to a QByteArray using QDataStream
I ended up using QBitArray.. it wasn't straight forward but it worked.