Thank very much. I've found the reason, it's because the serialized data the binary data instead of text, so when stored into a char array, it somehow won't appear textually, but in fact it is there.
i'm sorry to have disturbed the forum with such silly question. On Friday, June 9, 2017 at 1:14:32 AM UTC+8, Bo Yang wrote: > > http://www.cplusplus.com/reference/string/string/string/ > std::string(buf) will be initialized with null terminated string. If the > serialized data contains 0, it will be truncated in the string. > I think you should use std::string(buf, n) > > On Thu, Jun 8, 2017 at 9:57 AM David Zhao <[email protected] <javascript:>> > wrote: > >> another example: >> #include "client.h" >> #include "proto/uMemcached.pb.h" >> >> int main() { >> >> //Client client("127.0.0.1", 27777); >> //client.run(); >> >> mcmessage::Query query; >> query.set_type(mcmessage::Query::SET); >> query.set_key("good"); >> query.set_value("bad"); >> >> std::string str; >> char buf[1024] = {0}; >> >> query.SerializeToString(&str); >> query.SerializeToArray(buf, query.ByteSize()); >> >> std::cout << "str: " << str << std::endl; >> >> std::cout << "buf: " << buf << std::endl; >> } >> >> run it, get: >> >>> str:goodâ–’bad >>> buf: >>> >> >> the buf is not filled. >> why..... >> >> -- >> 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] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
