使用 Protocol Buffers 发送图标/小图像

发布于 2024-07-27 05:41:52 字数 886 浏览 4 评论 0原文

我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦欢 2024-08-03 05:41:52
const std::string s = icon();

const void *data=s.c_str();
const std::string s = icon();

const void *data=s.c_str();
星星的轨迹 2024-08-03 05:41:52

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.

许久 2024-08-03 05:41:52
void String2Image(string binFile,const char* outImage)
{
    fstream imgFile(outImage,ios::binary|ios::out);

    for(int i=0; i<binFile.length(); ++i) 
    {
        imgFile << binFile[i];
    }

    imgFile.close();
}
void String2Image(string binFile,const char* outImage)
{
    fstream imgFile(outImage,ios::binary|ios::out);

    for(int i=0; i<binFile.length(); ++i) 
    {
        imgFile << binFile[i];
    }

    imgFile.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文