示例文件main.proto

syntax = "proto3";
package main;

message Test {
    string comment = 1;
}

message Example {
    string name = 1;
    int32 age = 2;
    repeated Test t = 3;

    message Label {
        string source = 1;
    }
    repeated Label labels = 4;
}

示例文件main.txt

name: "Larry"
age: 99
t: [{comment: "hello"}, {comment: "world"}]
labels: [{source: "foo"}, {source: "bar"}]

protoc --encode编码

protoc --encode=main.Example main.proto < main.txt > main.bin

查看main.bin的内容

hexdump -C main.bin 

00000000 0a 05 4c 61 72 72 79 10 63 1a 07 0a 05 68 65 6c |..Larry.c....hel|
00000010 6c 6f 1a 07 0a 05 77 6f 72 6c 64 22 05 0a 03 66 |lo....world"...f|
00000020 6f 6f 22 05 0a 03 62 61 72 |oo"...bar|
00000029

protoc --decode编码

protoc --decode=main.Example main.proto < main.bin 

name: "Larry"
age: 99
t {
comment: "hello"
}
t {
comment: "world"
}
labels {
source: "foo"
}
labels {
source: "bar"
}

本文链接地址:https://const.net.cn/365.html

标签: protobuf, protoc

添加新评论