Getting most significant byte of qint64

c++, qt

Solution

This is more of a C/C++ question than Qt. But anyway:

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

Edit: To avoid messy endian specific position code:

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

note that you need to include `qendian.h` for this to work.

Problem

In QT I have a qint64. Is there an easy way of dividing this up into pieces of size int8_t? For clarity, if I had a ``` qint64 a = [11001000 00001111 11110000 ... 11001100] ``` I would like to get ``` int8_t a1=[11001000] int8_t a2=[00001111] int8_t a3=[11110000] ... int8_t a8=[11001100] ```

Original source