簡體   English   中英

來自 QtEndian 與 stdint.h 的沖突數據類型 quint16_be

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM