在 QT 中我有一个 qint64。有没有一种简单的方法可以将其分成大小为 int8_t 的片段?
为了清楚起见,如果我有一个
qint64 a = [11001000 00001111 11110000 ... 11001100]
我想得到
int8_t a1=[11001000]
int8_t a2=[00001111]
int8_t a3=[11110000]
...
int8_t a8=[11001100]
最佳答案
与 Qt 相比,这更像是 C/C++ 问题。但无论如何:
qint64 a = 56747234992934;
union {
qint64 i64;
int8_t i8[8];
} u = {a};
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
qDebug() << u.i8[0]; // MSB is the first byte on big endian machines
#else
qDebug() << u.i8[7]; // MSB is the last byte on little endian machines
#endif
编辑:为了避免乱七八糟的字节序特定位置代码:
qint64 a = 56747234992934;
union {
qint64 i64;
int8_t i8[8];
} u = {qToBigEndian(a)};
qDebug() << u.i8[0]; // MSB is the first byte on big endian machines
请注意,您需要包含 qendian.h
才能正常工作。
关于c++ - 获取qint64的最高有效字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13347186/