C++中組解數(shù)據(jù)包(序列化反序列化方式)-創(chuàng)新互聯(lián)

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;
	}
};

//對(duì)數(shù)據(jù)流包頭組包
templateQByteArray 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;
}

//對(duì)數(shù)據(jù)流進(jìn)行組包
templateQByteArray 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;
}

//對(duì)數(shù)據(jù)流進(jìn)行解包
templatevoid 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();
}

//對(duì)字符流進(jìn)行解包
templatevoid 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;      //時(shí)
		quint8 m_timeZoneCorrect;  //時(shí)區(qū)修正
		quint8 m_day;     //日
		quint8 m_month;   //月
		quint8 m_lowByteYear;   //年的低字節(jié)
		quint8 m_highByteYear;  //年的高字節(jié)

		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);		

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

作為一家“創(chuàng)意+整合+營(yíng)銷(xiāo)”的成都網(wǎng)站建設(shè)機(jī)構(gòu),我們?cè)跇I(yè)內(nèi)良好的客戶口碑。成都創(chuàng)新互聯(lián)公司提供從前期的網(wǎng)站品牌分析策劃、網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、創(chuàng)意表現(xiàn)、網(wǎng)頁(yè)制作、系統(tǒng)開(kāi)發(fā)以及后續(xù)網(wǎng)站營(yíng)銷(xiāo)運(yùn)營(yíng)等一系列服務(wù),幫助企業(yè)打造創(chuàng)新的互聯(lián)網(wǎng)品牌經(jīng)營(yíng)模式與有效的網(wǎng)絡(luò)營(yíng)銷(xiāo)方法,創(chuàng)造更大的價(jià)值。

文章名稱(chēng):C++中組解數(shù)據(jù)包(序列化反序列化方式)-創(chuàng)新互聯(lián)
當(dāng)前地址:http://muchs.cn/article34/idhpe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站改版、軟件開(kāi)發(fā)、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)