Thanks, I had no idea about the data method. Much to learn. Regards Saptarshi
On Aug 24, 2009, at 4:09 PM, Kenton Varda <ken...@google.com> wrote: > Yshould never use .c_str() in this case -- use .data() and .size() > instead. > > On Mon, Aug 24, 2009 at 12:58 PM, Saptarshi Guha <saptarshi.g...@gmail.com > > wrote: > Hello, > Thank you! Being new to c++ i had no idea of the size method. I was > thinking of strlen on c_str() which would have given me the wrong > number of bytes. > Nice and clean now. > Regards > Saptarshi > > > On Mon, Aug 24, 2009 at 3:21 PM, Kenton Varda<ken...@google.com> > wrote: > > 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 > <saptarshi.g...@gmail.com> 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 protobuf@googlegroups.com To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/protobuf?hl=en -~----------~----~----~----~------~----~------~--~---