正如您可能从标题中发现的那样,我在转换 QByteArray
时遇到了问题。为整数。
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。q_size
是 0x2500
(或其他字节序 - 0x0025
)。这里有什么问题?我很确定
q_size
保存我需要的数据。
最佳答案
这样的事情应该可以工作,使用数据流从缓冲区读取:
QDataStream ds(buffer);
short size; // Since the size you're trying to read appears to be 2 bytes
ds >> size;
// You can continue reading more data from the stream here
关于qt - QByteArray 到整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1251662/