COL*_*ICE 0 c# java floating-point bits
在C#中将位转换为float时,我输入的数字错误.
我们用这个位吧 number= 1065324597
在Java中,如果我想从bit转换为float,我会使用intBitsToFloat方法
int intbits= 1065324597;
System.out.println(Float.intBitsToFloat(intbits));
Run Code Online (Sandbox Code Playgroud)
输出:0.9982942将正确的输出我想在C#中获得
但是,在C#中我使用过
int intbits= 1065324597;
Console.WriteLine((float)intbits);
Run Code Online (Sandbox Code Playgroud)
输出:1.065325E+09 错!
我的问题是你如何在C#中转换inbitsToFloat?
我的尝试: 我查看了这里的文档http://msdn.microsoft.com/en-us/library/aa987800(v=vs.80).aspx 但我仍然有同样的麻烦
只是施法是一个完全不同的操作.您需要将BitConverter.ToSingle(byte[], int)其转换int为字节数组 - 并且可能会根据您想要的字节顺序颠倒顺序.(编辑:也许不需要这样,因为相同的字节顺序用于两个转换;任何不需要的字节顺序将刚修好本身)有BitConverter.DoubleToInt64Bits对double的,但没有直接的float等价物.
示例代码:
int x = 1065324597;
byte[] bytes = BitConverter.GetBytes(x);
float f = BitConverter.ToSingle(bytes, 0);
Console.WriteLine(f);
Run Code Online (Sandbox Code Playgroud)