Protocol Buffers - Encoding

1 encoding

Protocol Buffers Encoding
This document describes the binary wire format for protocol buffer messages. You don't need to understand this to use protocol buffers in your applications, but it can be very useful to know how different protocol buffer formats affect the size of your encoded messages.

1.1 A Simple Message

1.2 Base 128 Varints

key message

most significant bit(msb)

1.3 Message Structure

key message

wire-format
The available wire types are as follows:

| Type | Meaning | Used For |
| :---- | :-------- | |
| 0 | Varint | int32, int64, uint32, uint64, sint32, sint64, bool, enum |
| 1 | 64-bit | fixed64, sfixed64, double |
| 2 | Length-delimited | string, bytes, embedded messages, packed repeated fields |
| 3 | Start group | groups (deprecated) |
| 4 | End group | groups (deprecated) |
| 5 | 32-bit | fixed32, sfixed32, float |

Each key in the streamed message is a varint with the value (field_number << 3) | wire_type – in other words, the last three bits of the number store the wire type.

1.4 More Value Types

1.4.1 Signed Integers

key message

If you useint32orint64as the type for a negative number, the resulting varint isalways ten bytes long– it is, effectively, treated like a very large unsigned integer. If you use one of the signed types, the resulting varint uses ZigZag encoding, which is much more efficient.


In other words, each value n is encoded using

 (n << 1) ^ (n >> 31)

for sint32s, or

 (n << 1) ^ (n >> 63)

for the 64-bit version.

1.4.2 Non-varint Numbers

1.4.3 Strings

1.5 Embedded Messages

1.6 Optional And Repeated Elements

1.6.1 Packed Repeated Fields

Version 2.1.0 introduced packed repeated fields, which in proto2 are declared like repeated fields but with the special [packed=true] option. In proto3, repeated fields are packed by default.


For example, imagine you have the message type:

message Test4 {
 repeated int32 d = 4 [packed=true];
}```
> * * *
> Now let's say you construct a Test4, providing the values 3, 270, and 86942 for the repeated field d. Then, the encoded form would be:

22 // tag (field number 4, wire type 2)
06 // payload size (6 bytes)
03 // first element (varint 3)
8E 02 // second element (varint 270)
9E A7 05 // third element (varint 86942)

> Only repeated fields of primitive numeric types (types which use the varint, 32-bit, or 64-bit wire types) can be declared "packed".

##1.7 Field Order

其他文章
[译]Protocol Buffers Encoding
http://blog.csdn.net/andyelvis/article/details/7315464
Google Protocol Buffers 编码(Encoding) - 石头儿 - 博客园.html
http://www.cnblogs.com/shitouer/archive/2013/04/12/google-protocol-buffers-encoding.html
图解Protobuf编码 - zxh的专栏 - 博客频道 - CSDN.NET.html http://blog.csdn.net/zxhoo/article/details/53228303
primitive - Is there ever a good time to use int32 instead of sint32 in Google Protocol Buffers_ - Stack Overflow.html
http://stackoverflow.com/questions/765916/is-there-ever-a-good-time-to-use-int32-instead-of-sint32-in-google-protocol-buff
Missing value_null support for scalar value types in proto 3 · Issue #1606 · google_protobuf · GitHub.html
https://github.com/google/protobuf/issues/1606
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本来按照往常一样,直接连到电脑就可以抓包,但今天不行,所以今天就解决下这个问题 最近安装了nginx和php56,...
    Do_More阅读 6,990评论 0 0
  • 我的另外一篇将成型的iOS工程嵌入到u3d工程中 一 、运行环境 Unity 5.3.4Xcode 7.3 二、 ...
    朝雨晚风阅读 11,134评论 15 11
  • 行者顺达 波光微澜流向东, 洹河水面映日红。 两岸柳枝河中影, 更有高楼入云峰。
    行者顺达阅读 3,073评论 0 0

友情链接更多精彩内容