在 C# 中将位转换为浮点数时,我得到了错误的数字。
让我们使用这个位number= 1065324597
In Java,如果我想从位转换为浮点数,我会使用intBitsToFloat
method
int intbits= 1065324597;
System.out.println(Float.intBitsToFloat(intbits));
Output: 0.9982942
其中我想在 C# 中获得正确的输出
然而,在C# I used
int intbits= 1065324597;
Console.WriteLine((float)intbits);
Output: 1.065325E+09
错误的!!
我的问题是如何在 C# 中转换 inbitsToFloat ?
我的尝试:我查看了这里的文档http://msdn.microsoft.com/en-us/library/aa987800(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/aa987800(v=vs.80).aspx但我还是有同样的麻烦
只是铸造是一个entirely不同的操作。你需要BitConverter.ToSingle(byte[], int) http://msdn.microsoft.com/en-us/library/system.bitconverter.tosingle.aspx已经转换了int
到字节数组 - 并可能根据您想要的字节顺序颠倒顺序。 (编辑:可能不需要这样做,因为两种转换使用相同的字节序;任何不需要的字节序都会自行修复。)BitConverter.DoubleToInt64Bits
for double
,但没有直接的float
相等的。
示例代码:
int x = 1065324597;
byte[] bytes = BitConverter.GetBytes(x);
float f = BitConverter.ToSingle(bytes, 0);
Console.WriteLine(f);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)