Re: [protobuf] How to serialize/deserialize bytes data?

2016-12-22 Thread Henner Zeller
On 22 December 2016 at 14:50,   wrote:
> Thanks for the reply.
>
> My question here is: I use protobuffer to store a bytes data, how can I
> retrieve it back?

Symmetric to the SerializeToArray(), use ParseFromArray() or
ParseFromString() to parse a serialized version of a protocol buffer
back.

> In my previous example, just offset 2 from the protobuffer
> data? However, the size field may vary, right?

That code does not make any sense in the real world: you are starting
with a protocol buffer, then serialize the resulting string in a new
protocol buffer. I suspect that was only for testing something ?

>
> On Thursday, December 22, 2016 at 2:13:14 PM UTC-8, Adam Cozzette wrote:
>>
>> 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 for more information about the wire format.
>>
>> On Thu, Dec 22, 2016 at 1:52 PM,  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(, 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 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.
>>
>>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> 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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.


Re: [protobuf] How to serialize/deserialize bytes data?

2016-12-22 Thread 'Adam Cozzette' via Protocol Buffers
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  for
more information about the wire format.

On Thu, Dec 22, 2016 at 1:52 PM,  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(, 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 protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> 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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.


[protobuf] How to serialize/deserialize bytes data?

2016-12-22 Thread song1219
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(, 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 protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.