qt - QByteArray 到整数

标签 qt bytearray

正如您可能从标题中发现的那样,我在转换 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_size0x2500 (或其他字节序 - 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/

相关文章:

python - 将字节数组/MySQL BLOB 转换为 Python 中 float32 的列表/Numpy 数组?

c# - 如何将字符串转换为十六进制字节数组?

c++ - QT 应用程序在启动时崩溃 (c++ windows)

c++ - 如何确定 QString 中的特定字符是字母还是标点符号?

java - 将 Java 字符串转换为字节数组

android - Android 中 DatatypeConverter.printHexBinary(byte[] array) 和 DatatypeConverter.parseHexBinary(String str) 的替代方案

Java - 字节 [] 到字节 []

linux - 为 Qt 创建位图字体

c++ - 通过直接连接或排队连接调用插槽的 QTimer 超时插槽有什么区别?

qt - 使用QPainter绘制富文本