C++中组解数据包(序列化反序列化方式)
Posted 星火撩猿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++中组解数据包(序列化反序列化方式)相关的知识,希望对你有一定的参考价值。
class ExBodyHead
public:
quint8 m_no;
quint8 m_id;
quint16 m_length;
quint32 m_timestamp;
quint16 m_spare;
quint16 m_secondid;
ExBodyHead()
m_no = 0; m_id = 0; m_length = 0;
m_timestamp = 0; m_spare = 0; m_secondid = 0;
ExBodyHead(quint8 _no, quint8 _id, quint16 _length, quint16 _timestamp, quint16 _spare, quint16 _secondid)
: m_no(_no), m_id(_id), m_length(_length), m_timestamp(_timestamp), m_spare(_spare), m_secondid(_secondid)
friend QDataStream& operator<<(QDataStream& os, const ExBodyHead& data)
os << data.m_no << data.m_id << data.m_length << data.m_timestamp << data.m_spare << data.m_secondid;
return os;
friend QDataStream& operator>>(QDataStream& is, ExBodyHead& data)
is >> data.m_no >> data.m_id >> data.m_length >> data.m_timestamp >> data.m_spare >> data.m_secondid;
return is;
;
//对数据流包头组包
template<class T>
QByteArray PackProStruct(const T& data, bool bLittleEndian = false)
QByteArray headBytes;
QBuffer head_buffer(&headBytes);
head_buffer.open(QIODevice::WriteOnly);
QDataStream head_stream(&head_buffer);
head_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);
head_stream << data;
head_buffer.close();
return headBytes;
//对数据流进行组包
template<class T>
QByteArray PackProStruct(const T& data, ExPckgHead pckgHead, bool bLittleEndian = false)
QByteArray serial_array;
QBuffer serial_buffer(&serial_array);
serial_buffer.open(QIODevice::WriteOnly);
QDataStream out_stream(&serial_buffer);
out_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);
out_stream << data;
serial_buffer.close();
pckgHead.m_length = serial_array.size() + 16;
ExPckgHead head(pckgHead);
QByteArray headBytes;
QBuffer head_buffer(&headBytes);
head_buffer.open(QIODevice::WriteOnly);
QDataStream head_stream(&head_buffer);
head_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);
head_stream << head;
head_buffer.close();
serial_array.prepend(headBytes);
return serial_array;
//对数据流进行解包
template<class T>
void UnPackProStruct(const QByteArray& msg, T& data, bool bLittleEndian = false)
QByteArray deserial_array(msg);
QBuffer deserial_buffer(&deserial_array);
deserial_buffer.open(QIODevice::ReadOnly);
QDataStream in_stream(&deserial_buffer);
in_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);
in_stream >> data;
deserial_buffer.close();
//对字符流进行解包
template<class T>
void UnPackProStruct(const std::string& msg, T& data, bool bLittleEndian = false)
QByteArray deserial_array(msg.data(), msg.size());
QBuffer deserial_buffer(&deserial_array);
deserial_buffer.open(QIODevice::ReadOnly);
QDataStream in_stream(&deserial_buffer);
in_stream.setByteOrder(bLittleEndian ? QDataStream::LittleEndian : QDataStream::BigEndian);
in_stream >> data;
deserial_buffer.close();
class timeSysInfo : public ExBodyHead //BCD码
public:
quint8 m_second; //秒
quint8 m_minute; //分
quint8 m_hour; //时
quint8 m_timeZoneCorrect; //时区修正
quint8 m_day; //日
quint8 m_month; //月
quint8 m_lowByteYear; //年的低字节
quint8 m_highByteYear; //年的高字节
friend QDataStream& operator << (QDataStream& os, const timeSysInfo& data)
os << data.m_no << data.m_id << data.m_length << data.m_timestamp << data.m_spare << data.m_secondid
<< data.m_second << data.m_minute << data.m_hour << data.m_timeZoneCorrect << data.m_day
<< data.m_month << data.m_lowByteYear << data.m_highByteYear;
return os;
friend QDataStream& operator >> (QDataStream& is, timeSysInfo& data)
is >> data.m_no >> data.m_id >> data.m_length >> data.m_timestamp >> data.m_spare >> data.m_secondid
>> data.m_second >> data.m_minute >> data.m_hour >> data.m_timeZoneCorrect >> data.m_day
>> data.m_month >> data.m_lowByteYear >> data.m_highByteYear;
return is;
;
QByteArray datagram = EVDATA::PackProStruct(m_timeSysInfo, pckgHead);
以上是关于C++中组解数据包(序列化反序列化方式)的主要内容,如果未能解决你的问题,请参考以下文章
scala 将元组解包到案例类参数和附加的 zip 两个序列中