I have a binary data buffer which i want to store in a protocol buffer.
In the documentation (https://developers.google.com/protocol-buffers/docs/proto#scalar) it says that the bytes type is equivalent to string in C++. I could not believe this so i had to try it and yes, this seems to be the case..
This proto:
message BufferMsg {
required bytes buffer = 1;
}
gives a message definition containing this:
private:
::std::string* buffer_;
The public setter/getter API looks like this:
// required bytes buffer = 1;
inline bool has_buffer() const;
inline void clear_buffer();
static const int kBufferFieldNumber = 1;
inline const ::std::string& buffer() const;
inline void set_buffer(const ::std::string& value);
inline void set_buffer(const char* value);
inline void set_buffer(const void* value, size_t size);
inline ::std::string* mutable_buffer();
inline ::std::string* release_buffer();
inline void set_allocated_buffer(::std::string* buffer);
Surely, this cannot be the way to store binary data in a message. How should it be done? In C++ i would typically use an unsigned char array or something like that to store the data.
std::string? - Martin G'\0'instd::string. Length is tracked independently of that. - πάντα ῥεῖ