简体   繁体   中英

why protocol buffer bytes is string in c++?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM