为什么 flatbuffers 的输出与 C++ 在 Python 中不同?

Why is flatbuffers output different from C + + in Python?

我使用相同的协议文件,但我发现它们在 Python 和 C++ 中有不同的输出。 我的协议文件:


namespace serial.proto.api.login;

table LoginReq {
    account:string; //账号
    passwd:string; //密码
    device:string;  //设备信息
    token:string;
}

table LoginRsp {
    account:string; //账号
    passwd:string;  //密码
    device:string;  //设备信息
    token:string;
}

table LogoutReq {
    account:string;
}

table LogoutRsp {
    account:string;
}

我的python代码:

builder = flatbuffers.Builder()

account = builder.CreateString('test')
paswd = builder.CreateString('test')
device = builder.CreateString('test')
token = builder.CreateString('test')
LoginReq.LoginReqStart(builder)
LoginReq.LoginReqAddPasswd(builder, paswd)
LoginReq.LoginReqAddToken(builder, token)
LoginReq.LoginReqAddDevice(builder, device)
LoginReq.LoginReqAddAccount(builder, account)
login = LoginReq.LoginReqEnd(builder)
builder.Finish(login)
buf = builder.Output()
print(buf)
with open("layer.bin1","wb") as f:
    f.write(buf)

我的 C++ 代码:

flatbuffers::FlatBufferBuilder builder;
    auto account = builder.CreateString("test");
    auto device = builder.CreateString("test");
    auto passwd = builder.CreateString("test");
    auto token = builder.CreateString("test");
    auto l = CreateLoginReq(builder, account = account, passwd = passwd, device = device, token = token);
    builder.Finish(l);
    auto buf = builder.GetBufferPointer();
    flatbuffers::SaveFile("layer.bin", reinterpret_cast<char *>(buf), builder.GetSize(), true);

输出:

md5 layer.bin
MD5 (layer.bin) = 496e5031dda0f754fb4462fadce9e975

由不同实现(即生成器)生成的平面缓冲区不一定具有相同的二进制布局,但仍然可以等效。这取决于实现决定如何写出内容。因此,获取二进制文件的哈希值并不能告诉你等价性。