It looks like you are building up a protocol buffer containing a serialized
protocol buffer, which itself contains a serialized protocol buffer, which
in turn contains a serialized protocol buffer, etc. Each level of nesting
requires an additional two bytes: one byte for the tag number and a second
byte for the length of the byte string that follows.

For example, the first time you serialize it you need six bytes:
[ tag (one byte) ] [ size = 4 (one byte) ] [ ... 4 bytes of data ]

Then the second serialization looks like this (8 bytes total):

[ tag (one byte) ] [ size = 6 (one byte) ] [ ... the six bytes from above ]

See here <https://developers.google.com/protocol-buffers/docs/encoding> for
more information about the wire format.

On Thu, Dec 22, 2016 at 1:52 PM, <[email protected]> wrote:

> I defined a simple bytes message below
> message MfStream {
>   bytes message=1;
> }
>
> and run a test below, in which I use for a loop to keep
> serialize/deserialize the bytes message.
>
> char buf[1024];
> int tmp = 1;
> MfStream testMsg;
> testMsg.set_message(&tmp, sizeof(tmp));
> for (int i=0; i<5; i++) {
>     int size = testMsg.ByteSize();
>     testMsg.SerializeToArray(buf, sizeof(buf));
>     cout << "i=" << i << ", size=" << size << endl;
>     testMsg.set_message(buf, size);
> }
>
>
> The results below shows that the message size keeps increasing by 2 every
> loop. Why? How should I serialize/deserialize bytes data?
>
> i=0, size=6
> i=1, size=8
> i=2, size=10
> i=3, size=12
> i=4, size=14
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://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 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to