Is it possible to write out the 8-byte representation of a double number in C#, and then read in those 8 bytes in Java as the same double number that was encoded?
Yes, both java and .NET use IEEE-754 representations, although Java may omit some of the flags. The main question here is endianness, and that is trivial to reverse if needed. For example, in .NET you can handle this as:
double value = ...
byte[] bytes = BitConverter.GetBytes(value);
and:
byte[] bytes = ...
double value = BitConverter.ToDouble(bytes);
The endianness determines which byte goes first / last, but switching between them is as simple as reversing the array; for example, if you want "big-endian", then you could convert with:
if(BitConverter.IsLittleEndian) {
Array.Reverse(bytes);
}
(remembering to do that both when reading and writing)
I'm afraid I don't know the java for the same, but it is definitely fully available - I suspect ByteBuffer.putDouble / ByteBuffer.getDouble would be a good start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With