2012-02-07 88 views
0

我有以下protoc文件:C++谷歌协议缓冲器:指定二进制流protobuf的对象

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

字符串 “数据” 创建使用:

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

我的问题是:如何将字符串转换为protoc对象,以便我可以访问成员元素?请记住,我对C++很陌生。

回答

1

您可以使用

rb.ParseFromString(data) 
+0

问候,谢谢你。现在正在工作。对不起,迟来的回复。 – Eamorr 2012-02-07 13:46:32

2

在创建数据对象为什么不把:

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

然后在反序列化:

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

感谢 - 它的工作现在。 – Eamorr 2012-02-07 13:46:39