协议缓冲区:没有固定大小缓冲区的符号? [英] protocol buffers : no notation for fixed size buffers?

查看:122
本文介绍了协议缓冲区:没有固定大小缓冲区的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我没有得到关于 this 问题我必须建立原型并检查自己,因为我的数据集标题需要固定大小,我需要固定大小的字符串.那么,是否可以在协议缓冲区中指定固定大小的字符串或字节数组?这在这里并不明显,我对将固定大小的字符串强制放入标头消息感到有点难过.--即,std::string('\0', 128);

Since I am not getting an answer on this question I gotta prototype and check myself, as my dataset headers need to be fixed size, I need fixed size strings. So, is it possible to specify fixed size strings or byte arrays in protocol buffers ? It is not readily apparent here, and I kinda feel bad about forcing fixed size strings into the header message. --i.e, std::string('\0', 128);

如果不是,我宁愿使用 #pragma pack(1) struct header {...};'

If not I'd rather use a #pragma pack(1) struct header {...};'

编辑

问题间接回答这里.会回答,除了

Question indirectly answered here. Will answer and except

推荐答案

protobuf 在协议中没有这样的概念,在 .proto 模式语言中也没有.在字符串和 blob 中,数据总是技术上使用长度前缀的可变长度(它本身使用 varint 编码,因此即使长度也是可变长度).

protobuf does not have such a concept in the protocol, nor in the .proto schema language. In strings and blobs, the data is always technically variable length using a length prefix (which itself uses varint encoding, so even the length is variable length).

当然,如果你只存储特定长度的数据,那么它会排队.另请注意,由于 protobuf 中的字符串是使用 UTF-8 编码的 unicode,因此编码数据的长度不是像字符数那么简单(除非您仅使用 ASCII 字符).

Of course, if you only ever store data of a particular length, then it will line up. Note also that since strings in protobuf are unicode using UTF-8 encoding, the length of the encoded data is not as simple as the number of characters (unless you are using only ASCII characters).

这篇关于协议缓冲区:没有固定大小缓冲区的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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