why protocol buffer bytes is string in c++?

余生长醉 提交于 2019-11-29 17:12:05

问题


protocol buffer say it can contain any arbitrary sequence of bytes. but if my data contain '\0' ,how protocol buffer can encode my whole data from a string variable.


回答1:


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


来源:https://stackoverflow.com/questions/11467567/why-protocol-buffer-bytes-is-string-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!