首页 > 编程语言 > 详细

C++使用protobuf传输中间包含\0的字节数组

时间:2015-12-09 13:46:31      阅读:881      评论:0      收藏:0      [点我收藏+]

The C++ implementation of protocol buffers returns the byte and string types as std::string. This structure contains a length function telling you how long the corresponding data is (as well as the data itself.) Thus there is no special significance of embeded \0 characters.

The setting functions accept a string too, or there are versions that accept a buffer and length. If you want to set a field you can just do this:

pb.set_foo( std::string( data, data_length ));

or

pb.set_foo( data, data_length );

以上的方式在读取的时候,还是被转换为string(到第一个\0)
可以通过这样的方式解决:
pt.mutable_foo(),协议中再加上length即可

C++使用protobuf传输中间包含\0的字节数组

原文:http://www.cnblogs.com/shenzhigang/p/5032365.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!