qt-通信协议基础-double转成QbyteArray-小端系统

提问

8个字节 (64 bits) 双精度浮点数 (依据IEEE 754标准) ,Qt C++double怎么转成QbyteArray?

核心实现代码(C++/Qt)

cpp 复制代码
#include <QByteArray>
#include <QDebug>
#include <cstring>
// 基础版本:直接内存复制(系统字节序)
QByteArray doubleToQByteArray(double value) {
    QByteArray bytes;
    bytes.resize(sizeof(double));
    memcpy(bytes.data(), &value, sizeof(value));
    return bytes;
}
// 增强版本:可选字节序转换
QByteArray doubleToQByteArray(double value, bool bigEndian) {
    union {
        double d;
        quint64 i;
    } converter;
    converter.d = value;
    QByteArray bytes;
    bytes.resize(8);
    if (bigEndian) {
        // 大端序:高位在前
        bytes[0] = (converter.i >> 56) & 0xFF;
        bytes[1] = (converter.i >> 48) & 0xFF;
        bytes[2] = (converter.i >> 40) & 0xFF;
        bytes[3] = (converter.i >> 32) & 0xFF;
        bytes[4] = (converter.i >> 24) & 0xFF;
        bytes[5] = (converter.i >> 16) & 0xFF;
        bytes[6] = (converter.i >> 8)  & 0xFF;
        bytes[7] = converter.i        & 0xFF;
    } else {
        // 小端序:低位在前
        bytes[7] = (converter.i >> 56) & 0xFF;
        bytes[6] = (converter.i >> 48) & 0xFF;
        bytes[5] = (converter.i >> 40) & 0xFF;
        bytes[4] = (converter.i >> 32) & 0xFF;
        bytes[3] = (converter.i >> 24) & 0xFF;
        bytes[2] = (converter.i >> 16) & 0xFF;
        bytes[1] = (converter.i >> 8)  & 0xFF;
        bytes[0] = converter.i        & 0xFF;
    }
    return bytes;
}
// 反向转换验证
double qByteArrayToDouble(const QByteArray& bytes, bool bigEndian) {
    quint64 temp;
    if (bigEndian) {
        temp  = (static_cast<quint64>(bytes[0] & 0xFF) << 56);
        temp |= (static_cast<quint64>(bytes[1] & 0xFF) << 48);
        temp |= (static_cast<quint64>(bytes[2] & 0xFF) << 40);
        temp |= (static_cast<quint64>(bytes[3] & 0xFF) << 32);
        temp |= (static_cast<quint64>(bytes[4] & 0xFF) << 24);
        temp |= (static_cast<quint64>(bytes[5] & 0xFF) << 16);
        temp |= (static_cast<quint64>(bytes[6] & 0xFF) << 8);
        temp |= (static_cast<quint64>(bytes[7] & 0xFF));
    } else {
        temp  = (static_cast<quint64>(bytes[7] & 0xFF) << 56);
        temp |= (static_cast<quint64>(bytes[6] & 0xFF) << 48);
        temp |= (static_cast<quint64>(bytes[5] & 0xFF) << 40);
        temp |= (static_cast<quint64>(bytes[4] & 0xFF) << 32);
        temp |= (static_cast<quint64>(bytes[3] & 0xFF) << 24);
        temp |= (static_cast<quint64>(bytes[2] & 0xFF) << 16);
        temp |= (static_cast<quint64>(bytes[1] & 0xFF) << 8);
        temp |= (static_cast<quint64>(bytes[0] & 0xFF));
    }
    double result;
    memcpy(&result, &temp, sizeof(result));
    return result;
}
// 测试用例
int main() {
    double pi = 3.141592653589793;
    
    // 测试系统字节序(自动适应当前平台)
    QByteArray nativeBytes = doubleToQByteArray(pi);
    qDebug() << \"Native bytes (hex):\" << nativeBytes.toHex(' ');
    
    // 测试大端序(网络标准)
    QByteArray bigEndianBytes = doubleToQByteArray(pi, true);
    qDebug() << \"Big-endian bytes (hex):\" << bigEndianBytes.toHex(' ');
    
    // 反向验证
    double reversed = qByteArrayToDouble(bigEndianBytes, true);
    qDebug() << \"Reversed value:\" << reversed;
    
    return 0;
}

关键技术要点

  • 内存布局标准
    严格遵循IEEE 754双精度浮点数标准:
    • 1位符号位
    • 11位指数(偏移量1023)
    • 52位尾数(隐含前导1)
    • 示例值π的十六进制表示:
    • 大端序:40 09 21 FB 54 44 2D 18
    • 小端序:18 2D 44 54 FB 21 09 40
  • 字节序处理
    • 系统默认字节序:使用memcpy直接复制内存,保持与当前系统一致
    • 强制大端序:通过位移操作手动构造字节序,适用于网络传输和硬件通信
    • 自动检测字节序:可通过QSysInfo::ByteOrder获取当前系统字节序
  • 类型安全设计
    • 使用union进行类型双射,避免指针类型转换带来的未定义行为
    • 显式处理字节序参数,增强代码可读性和可维护性
  • 跨平台兼容性
    • 代码不依赖特定平台的字节序特性
    • 通过参数化设计支持不同场景需求
    • 兼容Windows/Linux/macOS等主流Qt平台

性能优化建议

  • 批量处理:对大量浮点数转换时,预分配QByteArray内存块,减少重复分配开销
  • 缓存机制:频繁转换相同值时可考虑结果缓存
  • SIMD优化:在支持SIMD的平台上,可使用Intel SSE或ARM NEON指令进行并行转换

典型应用场景

  • 传感器数据采集系统
  • 工业控制协议实现
  • 科学计算数据序列化
  • 音频/视频处理中的浮点数据存储
  • 硬件设备通信接口实现

其它资料

qt-通信协议基础-QStirng转QByteArray-进阶操作

相关推荐
web3.08889992 小时前
小红书笔记评论API接口详情展示
开发语言·笔记·python
手抄二进制2 小时前
使用Anaconda创建python环境并链接到Jupyter
开发语言·python·jupyter
古城小栈2 小时前
go-zero 从入门到实战 全指南(包的)
开发语言·后端·golang
lsx2024062 小时前
Python 3 函数
开发语言
-To be number.wan2 小时前
C++ 进阶技巧:如何让 cout << 自定义对象 正常输出?
开发语言·c++
2501_944446002 小时前
Flutter&OpenHarmony状态管理方案详解
开发语言·javascript·flutter
一路往蓝-Anbo2 小时前
C语言从句柄到对象 (三) —— 抛弃 Malloc:静态对象池与索引句柄的终极形态
c语言·开发语言·数据结构·stm32·单片机·算法
lbb 小魔仙3 小时前
【Java】Spring Data JPA 详解:ORM 映射、查询方法与复杂 SQL 处理
java·开发语言·sql·spring cloud
Fighting_p3 小时前
【预览word文档】使用插件 docx-preview 预览线上 word 文档
开发语言·c#·word