ProtoC Java Support of UINT32

1,824 views
Skip to first unread message

thatsn...@gmail.com

unread,
Sep 1, 2008, 2:02:59 PM9/1/08
to Protocol Buffers
We've been looking at using protocol buffers for a lot of our network
messages.

I'm curious why the decision to return a UINT32 message type as a java
int was made.

Really for correctness, I think a UINT32 data type should be treated
in Java as a long with the signed bits masked off.

Any comments?

Kenton Varda

unread,
Sep 2, 2008, 4:31:32 PM9/2/08
to thatsn...@gmail.com, Protocol Buffers
I think you could argue either way.  I went with int for consistency with uint64 (represented using long), and to make it clear that you cannot store more than 32 bits in the value.  If people feel strongly that it should be long instead, we could consider changing it, but I'm not sure if it would be worth the effort at this point.

thatsn...@gmail.com

unread,
Sep 3, 2008, 12:12:39 AM9/3/08
to Protocol Buffers
I would agree that this can go both ways.

The problem is Java's (asinine?) lack of support for unsigned value
types.

I have a patch that modifies the protoc compiler, and makes
appropriate modifications to the protoc java library itself, to
convert both FIXED32 and UINT32 to Long's, since we would prefer to
work with an unsigned 4 byte integer as a long. Obviously it was
worth the effort for our use.

By the way, some of the tests are a little bit strange, in that a -1
(technically) converts to the correct UINT32 value, but the true test
should be 2^32-1. There are a number of these test conditions I had
to modify in converting to Long instead of Integer.

chi...@gmail.com

unread,
Sep 3, 2008, 1:05:17 PM9/3/08
to Protocol Buffers
another trick would be to generate a little helper method for uint32
fields which gives you the actual value as a long. Something kinda
like:

long getFooUnsigned() {
return ((long) getFoo()) & 0xFFFFFFFF;
}

On Sep 2, 4:31 pm, "Kenton Varda" <ken...@google.com> wrote:
> I think you could argue either way.  I went with int for consistency with
> uint64 (represented using long), and to make it clear that you cannot store
> more than 32 bits in the value.  If people feel strongly that it should be
> long instead, we could consider changing it, but I'm not sure if it would be
> worth the effort at this point.
>

Kenton Varda

unread,
Sep 3, 2008, 2:31:52 PM9/3/08
to chi...@gmail.com, Protocol Buffers
On Wed, Sep 3, 2008 at 10:05 AM, hi...@hiramchirino.com <chi...@gmail.com> wrote:

another trick would be to generate a little helper method for uint32
fields which gives you the actual value as a long.  Something kinda
like:

Yeah, this seems considerably less invasive, though ugly.
Reply all
Reply to author
Forward
0 new messages