如何编写带有字节嵌入消息的proto文件

时间:2015-06-19 09:30:57

标签: google-api protocol-buffers protobuf-c

我有一个编码二进制文件如下

0a 16 0a 06 72 63 6e 33 31 72 12 0a 65 37 36 30 34 32 33 35 32 37 1a 00
20 01 2a 06 34 34 38 37 38

我不知道如何编写反映二进制文件的proto文件。我知道消息内容。

1. first two bytes indicate an embedded message with 22 bytes in length
2. inside the embedded message there are three fields (three strings)
3. then the rest of the message with two fields

如何编写.proto文件来反映上面的二进制文件?

message UserInfo
{
        required string username = 1;
        required string usserId  = 2;
        optional string extraInfo= 3;
}

message SendUserRequest
{
        required UserInfo uinfo = 1;
        ...
}

2 个答案:

答案 0 :(得分:0)

您提供的字节似乎缺少一个字节。我在结尾处添加了一个0,然后通过protoc --decode_raw运行并得到了这个:

1 {
  1: "rcn31r"
  2: "e760423527"
  3: ""
}
4: 1
5: "44878\000"  // (my added zero byte shows up here)

因此,除了您撰写的内容之外,您的SendUserRequest还应包含两个字段:

optional int32 a = 4;
optional string b = 5;

请注意,无法确定这些字段的正确名称是什么,也不知道它们是必需的还是可选的。而且,a在技术上可以是任何整数类型。

答案 1 :(得分:-1)

抱歉,这不是Protobuf的用途。 Protobuf只能解码Protobuf格式;它无法匹配任意ad-hoc字节格式。

编辑:我误解了这个问题。有时人们认为Protobufs可以描述任意二进制文件,并且给出的确切字节被protoc --decode_raw拒绝,但事实证明有一个丢失的字节。我在上面添加了一个更好的答案。