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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本來按照往常一樣,直接連到電腦就可以抓包,但今天不行,所以今天就解決下這個(gè)問題 最近安裝了nginx和php56,...
    Do_More閱讀 2,458評論 0 0
  • 我的另外一篇將成型的iOS工程嵌入到u3d工程中 一 、運(yùn)行環(huán)境 Unity 5.3.4Xcode 7.3 二、 ...
    朝雨晚風(fēng)閱讀 5,969評論 15 11
  • 行者順達(dá) 波光微瀾流向東, 洹河水面映日紅。 兩岸柳枝河中影, 更有高樓入云峰。
    行者順達(dá)閱讀 435評論 0 0

友情鏈接更多精彩內(nèi)容