On Wed, Mar 12, 2014 at 9:48 AM, Ke Wang <kewang...@gmail.com> wrote:
> Thanks, but when I print out the char* using string.data(), it got truncated
> too.

Right... most print functions will stop when they see a null
character... you can't use printf/cout/etc -- you'd have to write a
custom print mechanism that e.g. printed out hex. For example

void print_data(const std::string& str) {
  for (int i = 0; i < str.length(); i++) {
    if (i && (i % 20) == 0) printf("\n");
    printf("%02X ", str[i]);
  if (i % 20) printf("\n");

Or something along those lines. (I don't actually remember if str[i]
works, but if not, should be easy to do e.g. str.data()[i] or

> 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?

Check what data is sent over the socket with e.g. tcpdump and make
sure that it's the data you expect. Make sure that if you're never
handling the string as a char*. It needs to always be a (char *,
length) pair (which is essentially what std::string is). Otherwise the
implicit termination of c-style strings (0 byte) will break things.


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 http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to