QByteArray转换为整数 [英] QByteArray to integer
问题描述
您可能已经从标题中弄清楚了,我在将QByteArray
转换为整数时遇到问题.
As you may have figured out from the title, I'm having problems converting a QByteArray
to an integer.
QByteArray buffer = server->read(8192);
QByteArray q_size = buffer.mid(0, 2);
int size = q_size.toInt();
但是,size
为0.buffer
没有收到任何ASCII字符,并且我相信toInt()
函数如果不是ASCII字符将无法工作. int size
应该是37(0x25),但是-正如我所说的-它是0.
However, size
is 0. The buffer
doesn't receive any ASCII character and I believe the toInt()
function won't work if it's not an ASCII character. The int size
should be 37 (0x25), but - as I have said - it's 0.
q_size
是0x2500
(或其他字节顺序-0x0025
).
The q_size
is 0x2500
(or the other endianness order - 0x0025
).
这是什么问题?我很确定q_size
拥有我需要的数据.
What's the problem here ? I'm pretty sure q_size
holds the data I need.
推荐答案
我自己还没有尝试过看看它是否有效,但是从Qt文档中看起来像您想要一个QDataStream.这支持提取所有基本的C ++类型,并且可以使用QByteArray作为输入来创建.
I haven't tried this myself to see if it works but it looks from the Qt docs like you want a QDataStream. This supports extracting all the basic C++ types and can be created wth a QByteArray as input.
这篇关于QByteArray转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!