Quote Originally Posted by ChrisW67 View Post
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:
Qt Code:
  1. size_t hdrlen = sizeof(hdr);
  2. tx_msg->append(hdrlen & 0xff)
  3. hdrlen = hdrlen >> 8;
  4. 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):
Qt Code:
  1. uint16_t hdrlen = sizeof(hdr);
  2. tx_msg->append((char *) &hdrlen, sizeof(hdrlen));
To copy to clipboard, switch view to plain text mode 

You could also (carefully) use QDataStream.
Thanks Chris. I could figure out the first approach mentioned above.