我尝试过以下方法:
qDebug() << QByteArray("\x00\x10\x00\x00").size();我得到的是0而不是4个女巫。
什么是一个很好的数据类型来保存这4个字节的数据,因为我需要稍后将它们写入套接字,所以它们必须保持完全像你在上面看到的那样?
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 as i need to later write them to a socket so they must remain exactly like you see them above?
最满意答案
构造函数QByteArray(const char* str)在参数上使用qstrlen 。 由于您的字符串以0x00字节qstrlen ,因此qstrlen返回0,因此生成的QByteArray为0字节长。
要避免qstrlen检查,请使用QByteArray(const char* str, int size)构造函数:
qDebug() << QByteArray("\x00\x10\x00\x00", 4).size();将按预期打印4。
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.
更多推荐
发布评论