2

I have a double(float/Decimal) value and I want to get the same byte array as produced by Java ByteBuffer in C#. However the byte array produced by using ByteBuffer.PutDouble in Java and BinaryWriter in C# is different. Can someone please explain the implementation detail difference between the two.

Java:

ByteBuffer bytes = ByteBuffer.allocate(8).putDouble(0,1.12346);

bytes[] = {63, -15, -7, -83, -45, 115, -106, 54};

C#:

double value = 1.12346;
byte[] arr;
using (MemoryStream stream = new MemoryStream())
{
   using (BinaryWriter writer = new BinaryWriter(stream))
   {
      writer.Write(value);
      arr = stream.ToArray();
   }
}

arr[] = {153, 211, 101, 49, 177, 249, 241, 63};

xyz
  • 57
  • 1
  • 5

1 Answers1

1

ByteBuffer is big endian by default and bytes are signed.

In C#, it's little endian by default and bytes are unsigned.

You have the same data in the opposite order, from a serialization point of view, the sign of the bytes is not important except it is a little confusing.

In C# you can use the EndianBinaryWriter BinaryWriter Endian issue

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130