为什么协议缓冲区字节是c ++中的字符串? [英] why protocol buffer bytes is string in c++?

查看:151
本文介绍了为什么协议缓冲区字节是c ++中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

协议缓冲区,它可以包含任意任意字节序列。但如果我的数据包含'\0',协议缓冲区如何编码我从字符串变量的整个数据。

解决方案

协议缓冲区的C ++实现返回字节和字符串类型为 std :: string 。这个结构包含一个长度函数告诉你对应的数据有多长时间(以及数据本身。)因此,嵌入的 \0 字符没有特殊的意义。 / p>

设置函数也接受字符串,或者有接受缓冲区和长度的版本。
如果你想设置一个字段,你可以这样做:

  pb.set_foo(std :: string data,data_length)); 

  pb.set_foo(data,data_length); 


protocol buffer say it can contain any arbitrary sequence of bytes. but if my data contain '\0' ,how protocol buffer can encode my whole data from a string variable.

解决方案

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 );

这篇关于为什么协议缓冲区字节是c ++中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆