S
S
sddvxd2018-09-12 00:17:15
Qt
sddvxd, 2018-09-12 00:17:15

Why are the first 4 bytes of QString taken?

Hello
Reading the line "hello" I wrote in the QLineEdit widget:

QByteArray arrBlock;
    QDataStream bufferOut(&arrBlock, QIODevice::WriteOnly);
    bufferOut.setVersion(QDataStream::Qt_5_3);
    bufferOut << pLineEdit->text();

In debug mode, I look at the contents of the byte array (arrBlock):
0x00 0x00 0x00 0x0C - непонятная мне структура
        0x04 0x3f 0x04 0x40 0x04 0x38 0x04 0x32 0x04 0x35 0x04 0x42 - "привет" в UNICODE

Why did I need all this? I pass these bytes to the server and try to somehow display them on the screen:
QString str(byteArray);
    pTextEdit->append(str);

The bytes arrive safe and sound, but broken characters come out in QTextEdit, or rather, only the first character. I can’t understand, I take, I take the value of the type, I initialize it with the same type, but it doesn’t turn out the way I planned
. My assumption - while writing I thought that the first 4 bytes are reserved by QString to indicate the number of bytes to indicate the length of the string and this is what -something quint64 (long), because the string "hello" consists of 12 bytes, and 4 bytes in the QString data, namely 0C. Can you advise what to do in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Stolyarov, 2018-09-12
@sddvxd

It looks like the first 32 bits are allocated as a value indicating the length of the string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question