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即可