Qt QByteArray问题(Qt QByteArray issue)

编程入门 行业动态 更新时间:2024-10-25 09:28:04
Qt QByteArray问题(Qt QByteArray issue)

我尝试过以下方法:

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.

更多推荐

本文发布于:2023-08-04 16:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Qt   QByteArray   issue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!