3

C# で倍精度数の 8 バイト表現を書き出してから、Java でそれらの 8 バイトをエンコードされた同じ倍精度数として読み取ることは可能ですか?

4

2 に答える 2

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.

于 2013-04-26T21:39:29.240 に答える