Protobuf Java到C ++的序列化[二进制] [英] Protobuf Java To C++ Serialization [Binary]

查看:556
本文介绍了Protobuf Java到C ++的序列化[二进制]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,可以用Java中的Protobuf序列化数据,方法是将二进制数据写入byte []数组,然后将其保存在".txt"文件中.我正在stringstream中的C ++端接收该数据.现在,我想用C ++解析该二进制数据,但是Protobuf-Parsing-Method"parseFromString()"不起作用!我的测试消息中的字段未设置.我为此做了一些测试,可以向您展示一些代码:

I have a program that serializes data with Protobuf in Java, by writing binary data in a byte[] Array and then saving it in ".txt" file. I am receiving that data on the C++ side in a stringstream. Now I want to parse that binary data with C++, but the Protobuf-Parsing-Method "parseFromString()" doesn't work! The fields from my Test Message are not set. I wrote a little test for that and I can show you some code:

Java序列化

  byte[] s = test.build().toByteArray(); //This is serialized to "C:\test.txt" as binary

C ++解析:

Test t1; // My Protobuf Message
std::ifstream myFile("C:\\test.txt");
std::string s;
myFile >> s;

t1.ParseFromString(s);
std::cout << "Decoded: " << t2.s() << std::endl; // Check if parsing was correct

但是它只是返回:"Decoded:",好像t2是空的,但是不应该这样!如何在C ++中解析二进制数据?

But it just returns: " Decoded: ", as if t2 was empty, but it shouldn't be! How can you parse binary data in C++?

推荐答案

您的问题可能在这里:

myFile >> s;

>>运算符读取以空格分隔的 text 字符串.编码的protobuf不是文字. ParseFromString()可能返回false,表明它无法解析数据,因为它不完整.

The >> operator reads a text string, delimited by whitespace. An encoded protobuf is not text. Probably, ParseFromString() is returning false to indicate that it couldn't parse the data, because it is incomplete.

您想要做的就是读取整个文件.在这种情况下,最简单的方法是使用ParseFromIstream(&myFile). (并确保检查它是否返回true!)

What you want to do is read the whole file. The easiest way to do this in your case is to use ParseFromIstream(&myFile). (And make sure to check that it returns true!)

(另一种选择是检查文件大小,创建该大小的数组,使用myFile.read(array, size),然后使用ParseFromArray(array, size),但这将做同样的事情.)

(Another option would be to check the file size, create an array of that size, use myFile.read(array, size), and then ParseFromArray(array, size), but that's a lot more work that will do the same thing.)

请注意,您可能不应该将.txt用作protobuf的文件扩展名,因为该文件不包含文本.

Note that you probably should not use .txt as a file extension for a protobuf as the file does not contain text.

还请注意,令人困惑的是,在C ++中,您可以将二进制(非文本)数据放入std::string中-它只是一个字节容器-但是在Java中,您不能将二进制数据放入在String中.因此,C ++ ParseFromString()SerializeAsString()处理二进制数据,而Java toString()实际上返回消息的文本表示形式(用于调试目的),而不是您要传输的

Also note that, confusingly, in C++ you can put binary (non-text) data in an std::string -- it's just a byte container -- but in Java you cannot put binary data in a String. So the C++ ParseFromString() and SerializeAsString() deal in binary data, whereas the Java toString() actually returns a textual representation of the message (meant for debugging purposes) which is not what you want to transmit.

这篇关于Protobuf Java到C ++的序列化[二进制]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆