#include "stdafx.h"
#include <iostream>
using namespace std;
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
using namespace boost::archive;
#include <sstream>
int main()
{
int a = 10; // 4
string s("你好!"); // 序列化时字节是9
cout << s.size() << endl;// 5字节长度(2字节/汉字 + 4字节(成员大小))
uint32_t b = 23; // 4
string ss("abcdfrg"); // 序列化时字节是11(7字母 + 4字节(成员大小))
cout << ss.size() << endl;
int64_t c = 2456; // 8
// char、double会有问题
//char d = 'a';
//double e = 3.14;
std::stringstream os;
boost::archive::binary_oarchive arSend(os, boost::archive::no_header);
arSend << a << s << b << ss << c; //<< d << e;
cout << os.str().size() << endl;// 36
int a1 = 0;
string s1("");
uint32_t b1 = 0;
string ss1("");
int32_t c1 = 0;
//char d1 = 'h';
//double e1 = 0.0;
boost::archive::binary_iarchive arRecv(os, boost::archive::no_header);
arRecv >> a1;
cout << "a1: " << a1 << endl;//10
arRecv >> s1;
cout << "s1: " << s1 << endl;//你好!
arRecv >> b1;
cout << "b1: " << b1 << endl;//23
arRecv >> ss1;
cout << "ss1: " << ss1 << endl;//abcdfrg
arRecv >> c1;
cout << "c1: " << c1 << endl;//2456
//arRecv >> d1;
//cout << "d1: " << d1 << endl;
//arRecv >> e1;
//cout << "e1: " << e1 << endl;
return 0;
}
来源:CSDN
作者:ztq小天
链接:https://blog.csdn.net/weixin_38850997/article/details/88702229