On Sat, Dec 3, 2011 at 20:07, 萌 饶 <[email protected]> wrote: > I have a puzzle for a long time that why a negative int32 must be > encoded into 10 bytes instead of 5, 5 bytes of encoded var int has > enough bits(5*7=35) to represent a 32 bit value, and packet decoder > know it's a int32 so it extract 32 bits outof 5 bytes to restore the > int32 precisely, even if it's negtive.So why must protobuf encode > negative int32 to 10 bytes? Thank you.
The wiretype is not specified to int32 or int64, it is only specified to be a variable encoded integer. This way, things are upwards compatible if you change an int32 value to int64 later. But because of that, the varint encoding always encodes the value as int64 value; which requires 10 bytes for full encoding. If you have negative values, you should use the zigzag encoded values (sint32, sint64). -h -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
