Serialization 什么';protobuf数据的存储格式是什么?

Serialization 什么';protobuf数据的存储格式是什么?,serialization,protocol-buffers,Serialization,Protocol Buffers,.proto文件: package lm; message helloworld { required int32 id = 1; required string str = 2; optional int32 opt = 3; } writer.cc文件: #include <iostream> #include <string> #include "lm.helloworld.pb.h" #include <fstream>

.proto文件:

package lm;
message helloworld
{
     required int32 id = 1;
     required string str = 2;
     optional int32 opt = 3;
}
writer.cc文件:

#include <iostream>
#include <string>
#include "lm.helloworld.pb.h"
#include <fstream>
using namespace std;

int main()
{
    lm::helloworld msg1;
    msg1.set_id(101000);
    msg1.set_str("helloworld,this is a protobuf writer");
    fstream output("log", ios::out | ios::trunc | ios::binary);
    string _data;
    msg1.SerializeToString(&_data);
    cout << _data << endl;
    if(!msg1.SerializeToOstream(&output))
    {
        cerr << "Failed to write msg" << endl;
        return -1;
    }
    return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include "lm.helloworld.pb.h"
using namespace std;

void ListMsg(const lm::helloworld & msg)
{
    cout << msg.id() << endl;
    cout << msg.str() << endl;
}

int main(int argc, char* argv[])
{
    lm::helloworld msg1;
    {
        fstream input("log", ios::in | ios::binary);
        if (!msg1.ParseFromIstream(&input))
        {
            cerr << "Failed to parse address book." << endl;
            return -1;
        }
    }

    ListMsg(msg1);
    return 0;
}
#包括
#包括
#包括“lm.helloworld.pb.h”
#包括
使用名称空间std;
int main()
{
lm::helloworld msg1;
msg1.set_id(101000);
msg1.set_str(“helloworld,这是一个protobuf编写器”);
fstream输出(“log”,ios::out | ios::trunc | ios::binary);
字符串数据;
msg1.序列化字符串(&_数据);
cout

如果没有一个例子来说明另一端发生了什么,这就有点难以准确回答,但对于你所看到的有两种可能的解释:

  • 您已经在代码中显式地切换到;这是非常不可能的-事实上,
    TextFormat
    的主要用途是调试等
  • 更可能的情况是,您看到的只是二进制消息中的文本数据;文本编码为UTF-8,因此,如果您在文本编辑器中打开protobuf文件,其中的部分将显示为可读的,足以显示文件中的某些内容
  • 真正的问题是:输出文件中的实际字节数是多少?如果是:

    08-88-95-06-12-24-68-65-6C-6C-6F-77-6F-72-6C-64-2C-74-68-69-73-20-69-73-20-61-20-70-72-6F-74-6F-62-75-66-20-77-72-69-74-65-72

    然后是二进制格式;但请注意,其中大部分只是字符串的UTF-8
    “helloworld,这是一个protobuf编写器”
    ——它以绝对大小控制消息:

    68-65-6C-6C-6F-77-6F-72-6C-64-2C-74-68-69-73-20-69-73-20-70-72-6F-74-6F-62-75-66-20-77-72-69-74-65-72

    因此,如果您查看任何文本编辑器,它将在开头显示为几个乱码,后面是清晰易读的
    helloworld,这是一个protobuf writer

    此处的“二进制”是起始位:

    08-88-95-06-12-24

    这是:

    • 08:标题:字段1,变量
    • 88-95-06:值(十进制)101000作为变量
    • 12:标题:字段2,长度前缀
    • 24:作为变量的值(十进制)36(字符串的长度,以字节为单位)
    需要注意的要点:

    • 如果你的信息以文本为主,是的,它的大部分甚至以二进制形式看起来也是可读的
    • 看看开销;它需要6个字节来编码整个消息的其余部分,其中3个字节是数据(101000)——因此实际上只有3个字节作为开销丢失;现在比较并对比xml、json等,以了解protobuf是如何帮助您的