I have a Java SocketServer that sends doubles to a C# client. The sever sends the doubles with DataOutputStream.writeDouble()
and the client reads the double with BinaryReader.ReadDouble()
.
When I send dos.writeDouble(0.123456789);
and flush it from the server the client reads and outputs 3.1463026401691E+151 which is different from what I sent.
Are the C# and Java doubles each encoded differently?

- 147
- 2
- 3
- 8
-
1Both should be IEEE754, but byte order may differ. I'm not sure about C# though. – nanofarad Jun 18 '15 at 00:41
2 Answers
In Java, DataOutputStream.writeDouble()
converts the double to a long before sending, writing it High byte first (Big endian).
However, C#, BinaryReader.ReadDouble()
reads in Little Endian Format.
In other words: The byte order is different, and changing one of them should fix your problem.
The easiest way to change the byte order in Java from Big to Little Endian is to use a ByteBuffer, where you can specify the endian type: eg:
ByteBuffer buffer = ByteBuffer.allocate(yourvaluehere);
buffer.order(ByteOrder.LITTLE_ENDIAN);
// add stuff to the buffer
byte[] bytes = buffer.array();
Then, use DataOutputStream.write()

- 3,543
- 3
- 24
- 43
The issues is in fact with the encoding, specifically endianness. Java uses big endian format, which is the standard network endianness, while your C# client is using little endian format.
So here is what happened: 0.123456789
is stored in the IEEE754 Double precision format as 0x3FBF9ADD3739635F
. When this is read in C#, the byte order is switched, so it is stored as 0x5F633937DD9ABF3F
. This corresponds to the decimal number 3.14630264016909969143315814746e151
.
Take a look at this questing to see about reversing the byte order on the C# client side

- 1
- 1

- 300
- 1
- 3
- 14