Can I convert a Java double to 8 bytes using java.lang.Double.doubleToLongBits, write to network, then read those bits in C# using BitConverter.ToDouble?
我可以使用java.lang.Double.doubleToLongBits將Java double轉換為8字節,寫入網絡,然后使用BitConverter.ToDouble讀取C#中的那些位嗎?
Given the C# docs make no mention of IEEE 754 but do talk about endianness, indicating platform dependence, I suspect not in the general case. What if I know both Java and C# processes are running on Intel X64 architecture? With Java on Linux, C# on Windows. Any details would be interesting.
鑒於C#docs沒有提及IEEE 754,但確實談到了字節順序,表明平台依賴性,我懷疑不是一般情況。如果我知道Java和C#進程都在Intel X64架構上運行怎么辦?在Linux上使用Java,在Windows上使用C#。任何細節都會很有趣。
1 个解决方案
#1
2
It should be possible, C# does use IEEE 754 doubles. The only problem might be the endianness when transfering them over the network, so just choose the one your architecture is using and it should transfer correctly.
應該可以,C#確實使用IEEE 754雙打。唯一的問題可能是通過網絡傳輸時的字節序,所以只需選擇您的架構正在使用的那個,它應該正確傳輸。
Note, if you use a ByteBuffer, you can select the desired endian easily. Just use its double functions, no need to use doubleToLongBits -- that's just there in case you need the double representation to be converted to a long.
注意,如果使用ByteBuffer,則可以輕松選擇所需的字節序。只需使用它的雙重函數,不需要使用doubleToLongBits - 這就是你需要將雙重表示轉換為long的情況。