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.
Copyright Notice:Content Author:「Martin G」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42645047/storing-binary-data-buffer-in-protocol-buffer-message