简体   繁体   English

在 C++ 中从原始字节解码协议缓冲区

[英]Decode a protocol buffer from raw bytes in C++

I need to work with a protocol buffer input in my c++ function which is in raw bytes form.我需要在我的 c++ 函数中使用原始字节形式的协议缓冲区输入。 The tutorials inhttps://developers.google.com/protocol-buffers/docs/cpptutorial only mention serializing and parsing data to/from files. https://developers.google.com/protocol-buffers/docs/cpptutorial 中的教程只提到序列化和解析文件中的数据。 This is not my use case.这不是我的用例。

How can I decode a protocol buffer from raw bytes?如何从原始字节解码协议缓冲区? I only have a pointer to the data and the size to work with.我只有一个指向数据和大小的指针。 Preferably I don't want the data to be copied.最好我不想复制数据。

Google protobufs has a type bytes you could use. Google protobufs 有一个你可以使用的bytes类型。 If in your case you need to transform it to another scalar type, you'll have to do that before sending it to protobuffs as the library does some stuff behind the scenes to make sure it's in the right bight order at its destination.如果在您的情况下您需要将其转换为另一种标量类型,则必须在将其发送给 protobuff 之前执行此操作,因为库会在幕后执行一些操作以确保其在目的地处于正确的顺序。

seems to work now if I do it this way:如果我这样做,现在似乎可以工作:

    uint8* buffer = (uint8*) theBytesPnt;

    CodedInputStream* codedStream = new CodedInputStream(buffer, nBytes );
    EquityPredParams* message = new myGeneratedProtoClass();

    bool_success = message->ParseFromCodedStream(codedStream);

    magicallyObtainedNum = message->people(1).id();

where the people class is defined in the .proto file as in the google tutorial.在 .proto 文件中定义了 people 类,就像在谷歌教程中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM