简体   繁体   English

来自 QtEndian 与 stdint.h 的冲突数据类型 quint16_be

[英]conflict data type quint16_be from QtEndian with stdint.h

I want to use qint16_be, quint16_be, quint32_be etc. as a data type for big endian what I am trying is something like this我想使用 qint16_be、quint16_be、quint32_be 等作为大端的数据类型我正在尝试的是这样的

#include <QtEndian>
typedef quint16_be uint16_t;

And I am getting error我收到错误

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;

I have not included stdint.h in my project.我的项目中没有包含 stdint.h。 Any idea how would I resolve it知道我将如何解决它

Here we go: an MCVE for qToBigEndian() :这里我们 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: Output:

"\x01\x02"
"\x02\x01"

I ran this sample in VS2019 on my Intel laptop which has little-endian architecture.我在具有小端架构的英特尔笔记本电脑上在 VS2019 中运行了这个示例。
Respectively, the byte order was changed due to the call of qToBigEndian() .分别由于调用qToBigEndian()改变了字节顺序。

The conversion to QByteArray was a quick hack.转换为QByteArray是一个快速的技巧。
Nevertheless, conversion of const uint16_t* to const char* is allowed (with the intention to get access to the byte representation of something).不过,允许将const uint16_t*转换为const char* (目的是访问某些内容的字节表示)。

memcpy() is another permitted way to achieve this. memcpy()是实现此目的的另一种允许方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM