Thanks, but when I print out the char* using string.data(), it got truncated too. Later, I need to transfer this char* through TCP socket, which apparently cannot be decoded right at the server side. Did I do something wrong with the definition of the message?
Ke On Wednesday, March 12, 2014 5:28:50 AM UTC-5, Ilia Mirkin wrote: > > On Tue, Mar 11, 2014 at 11:09 PM, Ke Wang <[email protected] <javascript:>> > wrote: > > Hi all, > > > > I am using google protocol buffer to transmit complex data structures > over > > socket. Here is the .proto file: > > > > message MatrixMsg > > { > > required string msgType = 1; > > optional int64 count = 2; > > optional string extraInfo = 3; > > message TaskMsg > > { > > required string taskId = 1; > > required string user = 2; > > required string dir = 3; > > required string cmd = 4; > > required int64 dataLength = 5; > > } > > repeated TaskMsg tasks = 4; > > } > > > > I serialized a MatrixMsg to string through SerializeAsString(), and the > > string length is 500. Now, I want to convert the string to char* in > order to > > send it through socket. However, when I converted to char* through > > string.c_str(), the string got truncated. I printed out the string, and > > figured out there are white space in it. How do I get a char* that is > > exactly matches the string? Thanks! > > If you have a std::string, you can get at the data with str.data(), > and the length is str.length(). There can be null characters in the > output, so things like strlen won't produce useful results. [And > there's some subtle difference between .data() and .c_str() but I > don't quite remember what it is, and it might only be a theoretical > difference and not an actual one given the gcc implementation. My > memory is a little hazy on the topic though.] > > HTH, > > -ilia > -- 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 http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
