当前位置: 首页 > news >正文

bytes和基本数据类型之间的转换

点击查看代码
#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
http://www.aitangshan.cn/news/19.html

相关文章:

  • 糟糕,生产环境频繁Full GC,怎么办?
  • CSP/NOIP常用模板大全₍^˶⦁༝⦁˶^₎◞ ̑̑
  • 洛谷P1525 [NOIP 2010 提高组] 关押罪犯(恭喜解锁拆点并查集!!)
  • Score Matching
  • 对象转原始值
  • 通达信配色
  • I2C通信接口 VK2C22B 高抗干扰LED驱动段码液晶驱动芯片
  • 【自学嵌入式:stm32单片机】EXTI外部中断
  • Dify入门系列(1)| Dify 是什么?真能开启低代码 AI 应用开发?
  • 题解:P4368 [Code+#4] 喵呜
  • vue3 vue3-form-element表单生成工具
  • Codeforces 1042G Wafu! 题解 [ 绿 ] [ 数学 ] [ 线性 DP ] [ 前缀和 ] [ 暴力枚举 ]
  • 第二章:Linux基础命令
  • 题解:P4779 【模板】单源最短路径(标准版)
  • 事倍功半是蠢蛋39 cursor 报错user is unauthorized
  • 一个不错的AI写作工具
  • 2025CSP-S模拟赛33 比赛总结