Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why this length is 4? for python #353

Closed
nwaycn opened this issue May 8, 2015 · 1 comment
Closed

why this length is 4? for python #353

nwaycn opened this issue May 8, 2015 · 1 comment

Comments

@nwaycn
Copy link

nwaycn commented May 8, 2015

my proto file
message get_nway_gateways_req
{
required int32 start_pos = 1 ;
required int32 number_per_page = 2;
}
and protoc to python class
then I code it:

req_ng = gateway_op_pb2.get_nway_gateways_req()
req_ng.start_pos = 1
req_ng.number_per_page = 10
s_req_ng = req_ng.SerializeToString()
print len(s_req_nq)

the print is 4

@nwaycn nwaycn closed this as completed May 8, 2015
@jake-low
Copy link

jake-low commented May 8, 2015

For anyone reading this in the future, the structure of @nwaycn's message when serialized is something like this:

(5 bits + 3 bits) 8 bits (5 bits + 3 bits) 8 bits
field # + wire type varint field # + wire type varint

Two things may surprise you about this:

  • field number and wire type are combined into a single byte
  • an int32 doesn't (necessarily) take up 32 bits when serialized

Both of these are intentional design decisions of the protobuf serialization format. While they might not seem intuitive at first, they allow protobufs to be very space-efficient.

For more information, check out Google's protobuf encoding guide on the developer portal.

bithium pushed a commit to bithium/protobuf that referenced this issue Sep 4, 2023
A couple minor fixes. These are exercised by Ruby.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants