1 回答
TA贡献1963条经验 获得超6个赞
您需要颠倒字节的顺序。这与 Little Endian 和 Big Endian 之间的区别有关,即最低有效字节是先出现还是最后出现 - 您可以通过谷歌搜索了解更多信息。
Java以大端存储东西。如果你的系统是小端的,你需要在转换之前反转字节。BitConverter 提供了一种确定字节序的方法。例如:
// assuming we're starting with a big-endian byte[]
// we check if we're using little endian, and if so convert the byte[] to little endian (by reversing its order) before doing the double conversion
byte[] b = new byte[] { 64, 256-6, 256 - 51, 112, 256 - 93, 256 - 41, 10, 61 };
bool little = BitConverter.IsLittleEndian;
if (little)
{
byte[] nb = new byte[b.Length];
for(int i =0; i<b.Length; i++)
{
nb[i] = b[b.Length - 1 - i];
}
double doub = BitConverter.ToDouble(nb, 0);
}
else
{
double doub = BitConverter.ToDouble(b, 0);
}
- 1 回答
- 0 关注
- 278 浏览
添加回答
举报
