# Re: [protobuf] Question about data type from binary stream

```No I wrote some C code which will determine varint from that byte stream,
and give me a value of 150. What I was wondering was more along the lines
of what you're talking about here: "Of course there's no way to tell the
difference between, say, an int32 or sint32 (which uses zigzag encoding),
at least not from the information on the wire."```
```
If I know that the bytes are a type of varint from a byte stream, is there
then no way to determine whether the 150 should be calculated and
interpreted as an int32 or an sint32? That's my question. What exactly is
the TAG ID for?

On Wednesday, September 2, 2015 at 12:01:08 PM UTC-6, Ilia Mirkin wrote:
>
> \$ perl -ane 'foreach (@F) { print pack "C", hex(\$_) }' | protoc
> --decode_raw
> 08 96 01
> 1: 150
>
> Or did you want to see how one might do this by hand? The 96 is
> misleading since one might be tempted to think that 0x96 == 150 (which
> it is), but really it's 0x16 + high bit set, which indicates that the
> next bit should be taken from the next byte, which happens to just be
> 01, i.e. only the low bit is set, aka 0x96 in the end. If the stream
> were 08 96 02, then you should end up with a value of 0x116, i.e. 278:
>
> \$ perl -ane 'foreach (@F) { print pack "C", hex(\$_) }' | protoc
> --decode_raw
> 08 96 02
> 1: 278
>
> Everything in a protobuf stream is pairs of (tagid, value). The first
> byte has the tagid (if it's low enough, it's varint encoded starting
> with bit 3, so if it's > 4 bits it spills into the next byte) as well
> as the wiretype. The wiretype determines how to parse the value. Of
> course there's no way to tell the difference between, say, an int32 or
> sint32 (which uses zigzag encoding), at least not from the information
> on the wire.
>
> All this and more explained at
>
>
> Hope this helps.
>
>   -ilia
>
> On Sun, Aug 30, 2015 at 9:49 PM, TheAceInfinity <aceo...@gmail.com
> <javascript:>> wrote:
> > Say we take the bytes from the example in the docs.
> >
> > 0x08, 0x96, 0x01
> >
> > For 0x08, the wire_type is 0, which indicates VarInt, the message before
> > encoding the data shows that the VarInt is a type int32. The
> field_number
> > from 0x08 is 1 though. Where does this 1 come into play when it comes to
> > decoding the data from the binary stream?
> >
> > I read this: "As you know, a protocol buffer message is a series of
> > key-value pairs. The binary version of a message just uses the field's
> > number as the key"
> >
> > What is the definition of *key* in this context? Does it mean that I can
> use
> > this to determine the type from the specific wire_type as a key to be
> used
> > for a 1-based associative array? (i.e. 1 = int32, 2 = int64, etc...)
> >
> > If someone could just clarify how I would determine the specific VarInt
> out
> > of these 3 bytes, that would be much appreciated!
> >
> > --
> > 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 <javascript:>.
> > To post to this group, send email to prot...@googlegroups.com
> <javascript:>.
> > Visit this group at http://groups.google.com/group/protobuf.
> > For more options, visit https://groups.google.com/d/optout.
>

--
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