开发者

Qt QByteArray issue

开发者 https://www.devze.com 2023-01-19 22:03 出处:网络
I\'ve tried the following: qDebug() << QByteArray(\"\\x00\\x10\\x00\\x00\").size(); and i get 0 instead of 4 witch i would espect.

I've tried the following:

qDebug() << QByteArray("\x00\x10\x00\x00").size();

and i get 0 instead of 4 witch i would espect.

What would be a good data type to hold this 4 bytes of data开发者_StackOverflow as i need to later write them to a socket so they must remain exactly like you see them above?


The constructor QByteArray(const char* str) uses qstrlen on the argument. Since your string starts with a 0x00 byte, qstrlen returns 0, thus the resulting QByteArray is 0 bytes long.

To avoid the qstrlen check, use the QByteArray(const char* str, int size) constructor:

qDebug() << QByteArray("\x00\x10\x00\x00", 4).size();

will print 4 as you expect.

0

精彩评论

暂无评论...
验证码 换一张
取 消