#include <QCoreApplication>
#include "CommonFun.h"
#include <QtEndian>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "bytes转quint16";
{
// 方式一:移位转换
const QByteArray& byteBuf = QByteArray::fromHex("80f0");
quint16 nVal = 0;
FromBytesT(byteBuf.data(), nVal);
qDebug() << nVal;
}
{
// 方式二:QByteArray方法转换
const QByteArray& byteBuf = QByteArray::fromHex("80f0");
quint16 nVal = 0;
nVal = byteBuf.toHex().toUShort(nullptr, 16);
qDebug() << nVal;
}
{
// 方式三:memcpy转换
const QByteArray& byteBuf = QByteArray::fromHex("80f0");
quint16 nVal = 0;
memcpy(&nVal, byteBuf.data(), byteBuf.size()); // 此处会转成小端,因为x86为小端运行环境
nVal = qToBigEndian(nVal); // 转成大端
qDebug() << nVal;
}
{
// 方式四:qFromBigEndian方法转换
const QByteArray& byteBuf = QByteArray::fromHex("80f0");
quint16 nVal = 0;
qFromBigEndian<quint16>(byteBuf.data(), byteBuf.size(), &nVal);
qDebug() << nVal;
}
//------------------------------------------------------------------------
qDebug() << "quint16转bytes";
{
// 方式一:移位转换
quint16 nVal = 33008;
QByteArray byteBuf(8, '\0');
ToBytesT(nVal, byteBuf.data(), 2, false);
qDebug() << byteBuf.toHex(' ');
}
{
// 方式二:QByteArray方法转换
quint16 nVal = 33008;
QByteArray byteBuf = QByteArray::fromHex(QByteArray::number(nVal, 16));
qDebug() << byteBuf.toHex(' ');
}
{
// 方式三:memcpy转换
quint16 nVal = 33008;
QByteArray byteBuf(sizeof(quint16), '\0');
memcpy(byteBuf.data(), &nVal, sizeof(quint16));
std::reverse(byteBuf.begin(), byteBuf.end());
qDebug() << byteBuf.toHex(' ');
}
{
// 方式四:qToBigEndian方法转换
quint16 nVal = 33008;
QByteArray byteBuf(sizeof(nVal), '\0');
qToBigEndian<quint16>(nVal, byteBuf.data());
qDebug() << byteBuf.toHex(' ');
}
//------------------------------------------------------------------------
qDebug() << "QByteArray大小端转换";
{
QByteArray byteBuf = QByteArray::fromHex("80f0");
std::reverse(byteBuf.begin(), byteBuf.end()); // 逆转
qDebug() << byteBuf.toHex(' ');
}
qDebug() << "quint16大小端转换";
{
// 将给定的值转换为大端格式
quint16 nLittleEndianVal = 0xf080;
quint16 nValNew = qToBigEndian(nLittleEndianVal); // 当前为小端环境,会转成大端
qDebug() << nValNew;
}
return a.exec();
}
#ifndef COMMONFUN_H
#define COMMONFUN_H
#include <type_traits>
// 字节数据转基本类型
template <class T>
static void FromBytesT(const char* lpBuf, T& num, int startIndex = 0, bool bInputBigEndian = true)
{
const char* lpBytes = lpBuf + startIndex;
int size(sizeof(T));
char byMask(0xFF);
T temp(0);
num = 0;
if (bInputBigEndian)
{
for (int i = 0; i < size; ++i)
{
num <<= 8;
unsigned char byteValue = lpBytes[i] & byMask;
temp = (T)(byteValue);
num |= temp;
}
}
else
{
for (int i = size - 1; i >= 0; i--)
{
num <<= 8;
unsigned char byteValue = lpBytes[i] & byMask;
temp = (T)(byteValue);
num |= temp;
}
}
}
// 基本类型转字节数据
template <class T>
static void ToBytesT(const T& num, char *lpBuf, int startIndex = 0, bool bOutputBigEndian = true)
{
char* lpBytes = lpBuf + startIndex;
size_t size(sizeof (T));
if (bOutputBigEndian)
{
for (size_t i = 0; i < size; ++i)
{
lpBytes[i] = (char) (num >> (( (size - 1) - i ) * 8));
}
}
else
{
for (size_t i = 0; i < size; ++i)
{
lpBytes[i] = (char) (num >> (i * 8));
}
}
}
#endif // COMMONFUN_H