In C++, "bytes" fields are stored using std::string.  This class has a
size() method which returns the length of the string.  So, the length of
sdata1 is:  message.sdata1().size()
So you want to use option (A).

You would use option B if you wanted to store multiple independent byte
strings, each one containing an arbitrary number of bytes.  There is never a
reason to use option C.

On Mon, Aug 24, 2009 at 7:51 AM, Saptarshi <> wrote:

> Hello,
> Suppose I would like to store a type that could be a sequence of raw
> bytes, so
>    message ...{
>            optional bytes sdata1=1; //A
>            repeated bytes sdata2=2;  //B
>           optional BYT sdata3=3; //C
> }
> message BYT{
> uint32 length=1;
> bytes data=2;
> }
> Now I have a unsigned char * array which I wish to store in the
> message.
> My first approach was using (B), add_sdata2(array[i],i) (something
> similar), but this is 3bytes per byte stored.
> I then tried, option A, storing the entire set of data into sdata1
> (which is actually a string, according to the generated protobuf
> header files). But when it comes to reading it, how do i know the
> number if bytes stored in the string? Suppose I my data looks like
> 0x00,0x00,0x00, what will be the length?
> I am currently using option (c).
> Have I missed something? Is there a better approach.
> Thank you in advance
> Saptarshi
> >

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Reply via email to