Qt中的QByteArray和自定义结构体之间的相互转换(串口、TCP/UDP通讯)

时间:2021-09-07 15:14:39   收藏:0   阅读:18

在Qt项目开发中,经常会碰到自定义结构体和字符数组之间的转换问题,不妨假设结构体名字为custom_struct, 字符数组名字为array_data

1. QByteArray转换为自定义结构体

custom_struct *struct_data = reinterpret_cast<custom_struct *>(array_data.data());

 

或:强转

custom_struct *struct_data = (custom_struct *)array_data.data();

  

2. 自定义结构体转换为QByteArray

QByteArray array_data;
array_data.append((char*)&struct_data, sizeof(struct_data));

  

原文:https://www.cnblogs.com/xdl824/p/15237525.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!