C ++ uses protobuf to transmit a byte array containing \ 0 in the middle, and a protobuf Array

Source: Internet
Author: User

C ++ uses protobuf to transmit a byte array containing \ 0 in the middle, and a protobuf Array

The C ++ implementation of protocol buffers returns the byte and string typesStd: string. This structure contains a length function telling you how long the corresponding data is (as well as the data itself.) Thus there is no special significance of embeded\ 0Characters.

The setting functions accept a string too, or there are versions that accept a buffer and length. If you want to set a field you can just do this:

Pb. set_foo (std: string (data, data_length ));

Or

Pb. set_foo (data, data_length );

The above method is still converted to string (to the first \ 0) during reading)
You can solve the problem in this way:
Pt. mutable_foo (). Add length to the Protocol.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.