Has Anyone Successfully Defined "smaller" integers for protocol buffers (C++ or Java)?

389 views
Skip to first unread message

Douglas Lewis

unread,
May 24, 2016, 11:06:50 AM5/24/16
to Protocol Buffers
I'm working on a project and we are hoping to use protocol buffers for messages between embedded and PC based applications.

One issue I've immediately run into is protocol buffers only has 32 and 64 bit integers, I'm needing 16 and 8 bit integers for fields in the messages.   Has anyone been able to successfully add integer types to protocol buffers?  We are using C++ on the embedded side and Java on the PC side.

Thanks,
Doug

Ilia Mirkin

unread,
May 24, 2016, 11:09:49 AM5/24/16
to Douglas Lewis, Protocol Buffers
Non-fixed integers have varint encoding, so it doesn't matter how many
bits there are... or are you talking about fixed integers?

-ilia

Douglas Lewis

unread,
May 24, 2016, 11:32:00 AM5/24/16
to Protocol Buffers
I'm talking about fixed integers.  We have certain elements of the messages that we only need/want resolution to 16 bits or 8 bits.

- Doug

Ilia Mirkin

unread,
May 24, 2016, 11:39:17 AM5/24/16
to Douglas Lewis, Protocol Buffers
Why not just use a varint? You can use the zigzag variants if you want
to store things like -1 efficiently. The only time you'd benefit from
having a fixed8/fixed16 field is if the high bits are set, and it'd
only be by 1 byte.

-ilia
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+u...@googlegroups.com.
> To post to this group, send email to prot...@googlegroups.com.
> Visit this group at https://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.

Douglas Lewis

unread,
May 24, 2016, 12:24:06 PM5/24/16
to Protocol Buffers
Well the solution we want to implement is with 16 bit and 8 bit elements when that is all the resolution of the integer variable we need.  This is an embedded application and we are sending the messages over a wireless connection so data packet size is critical to achieving our desired throughput.

- Doug

On Tuesday, May 24, 2016 at 10:06:50 AM UTC-5, Douglas Lewis wrote:

Ilia Mirkin

unread,
May 24, 2016, 12:31:52 PM5/24/16
to Douglas Lewis, Protocol Buffers
If your varint is < 128, it will only take a single byte to get
encoded. Even if it's declared as an int64. (Also, consider that what
matters isn't data size but number of frames sent.)

Anyways, if you wanted to created fixed 8- and 16-bit types, you'd
have to add new field types for those, and pipe through the new types
everywhere. Just grep for FIXED32 and Uint32. However you really
should consider just using one of the varint-encoded types -- they
will likely serve you better.

-ilia
Reply all
Reply to author
Forward
0 new messages