[英]conflict data type quint16_be from QtEndian with stdint.h
我想使用 qint16_be、quint16_be、quint32_be 等作为大端的数据类型我正在尝试的是这样的
#include <QtEndian>
typedef quint16_be uint16_t;
我收到错误
C:/Qt/5.15.2/mingw81_64/include/QtCore/qendian.h:429:28: error: conflicting declaration 'typedef QBEInteger<short int> qint16_be'
typedef QBEInteger<qint16> qint16_be;
C:/Qt/Tools/mingw810_64/x86_64-w64-mingw32/include/stdint.h:38:25: note: previous declaration as 'typedef short unsigned int qint16_be'
typedef unsigned short qint16_be;
我的项目中没有包含 stdint.h。 知道我将如何解决它
这里我们 go: qToBigEndian()的 MCVE:
#include <QtCore>
int main()
{
const uint16_t sample = 0x0201;
QByteArray data((const char*)&sample, sizeof sample);
qDebug() << data;
const uint16_t sampleBE = qToBigEndian(sample);
QByteArray dataBE((const char*)&sampleBE, sizeof sampleBE);
qDebug() << dataBE;
}
Output:
"\x01\x02"
"\x02\x01"
我在具有小端架构的英特尔笔记本电脑上在 VS2019 中运行了这个示例。
分别由于调用qToBigEndian()
改变了字节顺序。
转换为QByteArray
是一个快速的技巧。
不过,允许将const uint16_t*
转换为const char*
(目的是访问某些内容的字节表示)。
memcpy()是实现此目的的另一种允许方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.