There is no QByteArray function to append() anything other than chars (bytes).
The sizeof(hdr) is a size_t with the value 4 (probably). size_t is variously defined, but must be at least 16-bits long and often an unsigned 32 or 64-bit integer.
At line 27 you append a single
char with the value stored in the size_t (it is an implied cast). One byte in the array is what you get. As a length value it will be broken if the hdr ever exceeds 255 bytes.
If you want to write a multi-byte integer using only QByteArray then you need to code breaking it into bytes and appending them in the correct order (whichever way round that is). Something like:
size_t hdrlen = sizeof(hdr);
tx_msg->append(hdrlen & 0xff)
hdrlen = hdrlen >> 8;
tx_msg->append(hdrlen & 0xff)
size_t hdrlen = sizeof(hdr);
tx_msg->append(hdrlen & 0xff)
hdrlen = hdrlen >> 8;
tx_msg->append(hdrlen & 0xff)
To copy to clipboard, switch view to plain text mode
or you can go this way (making the result platform architecture sensitive... your other code already is):
uint16_t hdrlen = sizeof(hdr);
tx_msg->append((char *) &hdrlen, sizeof(hdrlen));
uint16_t hdrlen = sizeof(hdr);
tx_msg->append((char *) &hdrlen, sizeof(hdrlen));
To copy to clipboard, switch view to plain text mode
You could also (carefully) use QDataStream.
Bookmarks