diff options
Diffstat (limited to 'endianhelpers.h')
-rw-r--r-- | endianhelpers.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/endianhelpers.h b/endianhelpers.h new file mode 100644 index 0000000..f11c669 --- /dev/null +++ b/endianhelpers.h @@ -0,0 +1,27 @@ +#ifndef ENDIANHELPERS_H +#define ENDIANHELPERS_H + +#include <QtCore/QtEndian> + +namespace +{ + +template<typename T> +inline T read(const QByteArray &data, int &offset) +{ + T unswapped; + qMemCopy(&unswapped, &data.constData()[offset], sizeof(T)); // Unaligned access warning! + offset += sizeof(T); + return qFromBigEndian<T>(unswapped); +} + +template<typename T> +inline void append(QByteArray &data, const T &value) +{ + T swapped = qToBigEndian<T>(value); + data.append(reinterpret_cast<const char*>(&swapped), sizeof(T)); +} + +} + +#endif // ENDIANHELPERS_H |