使用 Protocol Buffers 发送图标/小图像
我有一个关于 std::string 和谷歌协议缓冲区库的简单问题。 我已经定义了这样的消息:
message Source
{
required string Name = 1;
required uint32 Id = 2;
optional string ImplementationDLL = 3;
optional bytes Icon = 4;
}
我想使用 Icon 字段发送图像,它很可能是 png 图像。 将其提供给 protobuf 编译器后,我得到了类似的东西来访问/操作 Icon 字段。
inline bool has_icon() const;
inline void clear_icon();
static const int kIconFieldNumber = 4;
inline const ::std::string& icon() const;
inline void set_icon(const ::std::string& value);
inline void set_icon(const char* value);
inline void set_icon(const void* value, size_t size);
inline ::std::string* mutable_icon();
std::string* mutable_icon() 函数让我很头疼。 它返回一个 std::string 但我相信字符串不能保存二进制数据! 或者他们可以吗?
我可以使用 set_icon(const void*, size_t) 函数来放置二进制数据,但是如何在另一侧获取它?
我认为 std::string 可能能够保存二进制数据,但是如何???
I have a simple question about std::string and google's protocol buffers library.
I have defined a message like so:
message Source
{
required string Name = 1;
required uint32 Id = 2;
optional string ImplementationDLL = 3;
optional bytes Icon = 4;
}
I want to use the Icon field to send an image, it most probably will be a png image.
After feeding this to the protobuf compiler i got something like this to access/manipulate the Icon field.
inline bool has_icon() const;
inline void clear_icon();
static const int kIconFieldNumber = 4;
inline const ::std::string& icon() const;
inline void set_icon(const ::std::string& value);
inline void set_icon(const char* value);
inline void set_icon(const void* value, size_t size);
inline ::std::string* mutable_icon();
the std::string* mutable_icon() function is giving me a headache. It is returning a std::string but i believe strings can not hold binary data ! or can they ?
i can use set_icon(const void*, size_t) function to put binary data, but then how do i get it on the other side ?
i think std::string might be able to hold binary data, but how ????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ 字符串明确表示长度,因此它们可以包含二进制数据,包括终止 C 字符串的 0 字符。
您需要避免将字符串传递给需要 C 样式字符串的函数,因为它们不会处理嵌入的 0 字符。
C++ strings represent the length explicitly, so they can contain binary data, including the 0-character that terminates C strings.
You need to avoid passing the string to functions expecting a C-style string, since they won't handle embedded 0-chars.
这个问题的答案: 如何构造 std :: 嵌入 null 的字符串?
the answer to this question: How do you construct a std::string with an embedded null?