我正在尝试将 Java 函数转换为 C#,但我无法弄清楚。该函数应该将字节数组转换为双精度。
假设这两个函数应该做同样的事情,但他们没有。
我曾尝试在 C# 中使用 BitConverter,但这只会返回错误的双精度。
static double readBytes(RandomAccessFile in){
byte a, b, c, d, e, f, g, h;
a = in.readByte();
b = in.readByte();
c = in.readByte();
d = in.readByte();
e = in.readByte();
f = in.readByte();
g = in.readByte();
h = in.readByte();
byte[] ba = { h, g, f, e, d, c, b, a };
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(ba));
double x = dis.readDouble();
return x;
}
转换后的 C# 函数:(这个返回错误的双精度)
protected internal static double readBytes(FileStream @in)
{
byte a, b, c, d, e, f, g, h;
a = (byte)@in.ReadByte();
b = (byte)@in.ReadByte();
c = (byte)@in.ReadByte();
d = (byte)@in.ReadByte();
e = (byte)@in.ReadByte();
f = (byte)@in.ReadByte();
g = (byte)@in.ReadByte();
h = (byte)@in.ReadByte();
byte[] ba = { h, g, f, e, d, c, b, a };
double doub = BitConverter.ToDouble(ba, 0);
return doub;
}
对于字节数组 ={64, -6, -51, 112, -93, -41, 10, 61}在 Java 中我得到 double = 109783.04 (这是正确的转换),在 C# 中我得到 1.19203925203128E-14
神不在的星期二
相关分类