SerializeToString & SerializeToArray
syntax = “proto3”;
package test;
message People {
string name = 1;
int32 age = 2;
int32 steps = 3;
}
protoc Usage :
water@waters-MacBook-Pro:/Documents/GitHub/CppQt5/TestProtobuf$ protoc –cpp_out=. people.proto/Documents/GitHub/CppQt5/TestProtobuf$ ls people.p*
water@waters-MacBook-Pro:
people.pb.cc people.pb.h people.proto
#include #include “people.pb.h”
int main(int argc, char *argv[])
{
test::People pep;
pep.set_name(“heheda”);
pep.set_age(5);
pep.set_steps(1304);
std::string buff;
pep.SerializeToString(&buff);
test::People pep2;
pep2.ParseFromString(buff);
std::cout << pep2.name() << std::endl;
std::cout << pep2.age() << std::endl;
std::cout << pep2.steps() << std::endl;
int data\_len = pep.ByteSize();
unsigned char data\[1024\] = "\\0";
pep.SerializeToArray(data, data\_len);
for(auto i = 0; i < data\_len; i ++)
printf("%02X ", data\[i\]);
printf("\\n");
test::People pep3;
pep3.ParseFromArray(data, data\_len);
std::cout << pep3.name() << std::endl;
std::cout << pep3.age() << std::endl;
std::cout << pep3.steps() << std::endl;
return 0;
}