So far, I have used protocol buffers to serialize and deserialize objects using code-generated classes.
Now I am trying to serialize and deserialize a single 64-bit integer. The problem is that I get different results in Java and C #.
This is how I do it in Java ....
private static byte[] convertLongToByteArray(long value) throws IOException {
int size = CodedOutputStream.computeInt64SizeNoTag(value);
byte[] buffer = new byte[size];
CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(buffer);
codedOutputStream.writeInt64NoTag(value);
codedOutputStream.flush();
codedOutputStream.checkNoSpaceLeft();
return buffer;
}
And here is how I do it in C # using protobuf.net:
public void SerializeLongValue()
{
long n = 9876;
byte[] memoryBuffer = null;
using (MemoryStream destination = new MemoryStream())
{
ProtoBuf.Serializer.Serialize(destination, n);
destination.Flush();
memoryBuffer = destination.ToArray();
}
using (MemoryStream source = new MemoryStream(memoryBuffer))
{
long result = ProtoBuf.Serializer.Deserialize<long>(source);
Assert.AreEqual(n, result);
}
}
Java code converted number 9876
to[0x94, 0x4D]
C # code converts a number 9876
to[0x08, 0x94, 0x4D]
How to do it, how to com.google.protobuf
, and protobuf.net
were given identical outputs?