簡體   English   中英

C ++ Google協議緩沖區:將二進制流分配給protobuf對象

[英]C++ Google Protocol Buffers: assign binary stream to protobuf object

我有以下協議文件:

message DataChunk{
    required bool isHash=1;
    required int64 hash=2;
    required string data=3;
}

message responseBody{
    repeated DataChunk dataChunk=1;
}

而且我有以下C ++函數:

void eamorr(string data){   //data is a protocol buffer stream converted to a string
    responseBody rb;

    rb=some_function_of(data);   //what to do here?
}

字符串“ data”是使用以下命令創建的:

...
std::ostringstream stream;
rb.SerializeToOstream(&stream);
string protobufStream = stream.str();
...

我的問題是:如何將字符串轉換為協議對象,以便可以訪問成員元素? 請記住,我是C ++的新手。

創建數據對象時,為什么不做:

responseBody rb; //this is your proto object;
rb.SerializeToString(&data);

然后反序列化:

void eamorr(string data){
    responseBody rb;
    rb.ParseFromString(data);
}

您可以使用

rb.ParseFromString(data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM