3

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?

Nicholas Hill
  • 306
  • 2
  • 18
  • 2
    Wouldn't the string representations be the same? Ie, `Double.parseDouble("");` – Mike Christensen Apr 26 '13 at 21:23
  • You mean write it out to a file? – Eric Lippert Apr 26 '13 at 21:23
  • why don't you try it and see if it works – Sam I am says Reinstate Monica Apr 26 '13 at 21:24
  • 2
    If you're writing it out in bytes, you probably need to figure out how to deal with the difference between bytes between the two languages. Then you'll want to know if Java and C# agree on what the bits in a double actually mean. And then if they do, you have to write the code that writes to and reads from some shared buffer. You're home free! – Anthony Pegram Apr 26 '13 at 21:27
  • 2
    @AnthonyPegram I would *hope* (perhaps beyond reason) that both can handle IEEE 754; then the main question becomes simple: big-endian or little-endian? – Marc Gravell Apr 26 '13 at 21:31
  • I suppose my question centers on whether or not the bit representation of a double in C# (Windows) and Java (Android) are the same, or if there is a method in C#, or Java, to write/read each others' representations. – Nicholas Hill Apr 26 '13 at 21:32
  • @NicholasHill: The *internal* bit representation will be the same; the question is *what order those bits get written out to disk* by default. Do you understand what is meant by "endianness"? – Eric Lippert Apr 26 '13 at 21:37
  • Yes, I do get "endianness", but I'm hopeless at trying to understand floating point formats. In fact, I have previously been able to set endianness on a Java ByteBuffer in order to read in shorts and ints. – Nicholas Hill Apr 26 '13 at 21:43
  • @EricLippert - Ah interesting. I kinda assumed if you stripped away any cultural specific information and just allowed `[0-9]` and perhaps a leading negative sign, you'd be good. However, this question seems to be about byte encoding so I guess it's irrelevant now. – Mike Christensen Apr 26 '13 at 21:45

2 Answers2

7

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • ByteBuffer.order() allow syou to set the order, e.g. ByteOrder.nativeOrder() or ByteOrder.LITTLE_ENDIAN is usually good for .NET – Peter Lawrey Apr 26 '13 at 21:41
0

You can write a double as text or binary and write it using C#, Java and dozens of languages and you can read it in any number of languages as well. I don't know about lolcode. ;)

same double number that was encoded?

Technically is not encoded if you are using the native format as that is the binary value as it is used. i.e. no translation is required to use it.

To read a double read or write in native format I suggest using ByteBuffer setting the ByteOrder to LITTLE_ENDIAN.

e.g. To use the IEEE-754 representation which your CPU and most languages support (as the CPU is doing most of the real work)

 byte[] bytes = new byte[8];
 ByteBuffer bb = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
 double d = bb.getDouble();
 // or
 bb.putDouble(d);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130