The QDataStream writes enough information to reconstruct the item being written. When you write a byte, short, or int you get exactly 1, 2, or 4 bytes. Strings are written with an int indicating the length of the following string, in my example this is 12 bytes (6 x 2bytes per Unicode character). If you want single byte chars you should convert the QString to a char *, but you will still get the leading int indicating size.
data<< quint16(0xBEEF) <<quint8('S');
data<< message.toAscii();
data<<(quint8('L'));
qDebug() << arr.toHex();
data.device()->seek(0);
data << quint16(arr.size() - sizeof(quint16));
qDebug() << arr.toHex();
data.device()->seek(0);
...
"beef53000000064142434445464c"
"000c53000000064142434445464c"
QDataStream data(&arr,QIODevice::ReadWrite);
data.setVersion(QDataStream::Qt_4_6);
data<< quint16(0xBEEF) <<quint8('S');
data<< message.toAscii();
data<<(quint8('L'));
qDebug() << arr.toHex();
data.device()->seek(0);
data << quint16(arr.size() - sizeof(quint16));
qDebug() << arr.toHex();
data.device()->seek(0);
...
"beef53000000064142434445464c"
"000c53000000064142434445464c"
To copy to clipboard, switch view to plain text mode
If you want absolute control of the bytes in the stream you probably don't want QDataStream; look into QBuffer and QByteArray
Bookmarks