所以我有这个二进制文件,它存储了我需要读入数组的半浮点值。
00 BC 00 00 DD C4
Where the first two bytes represent = -1.,
3rd & 4th = 0.,
5th & 6th = -4.86328125,
我想将它们读入如下所示的数组中,{-1.000000, 0.000031, -4.863281}
如何做到这一点的任何线索?
附言。我知道 C# 不直接处理半浮点数。
检查下面的库,它实现了一个Half
类来支持半浮点数。
http://sourceforge.net/p/csharp-half/code/HEAD/tree/
用法:(顺便说一句,第二个数字是 0 而不是 0.000031)
byte[] array = new byte[]{
0x00, 0xBC, 0x00, 0x00, 0xDD, 0xC4
};
Half h1 = Half.ToHalf(array, 0);
Half h2 = Half.ToHalf(array, 2);
Half h3 = Half.ToHalf(array, 4);
float f1 = (float)h1;
float f2 = (float)h2;
float f3 = (float)h3;
Console.WriteLine("h1 = {0}; f1 = {1}", h1, f1);
Console.WriteLine("h2 = {0}; f2 = {1}", h2, f2);
Console.WriteLine("h3 = {0}; f3 = {1}", h3, f3);
/* outputs
*
* h1 = -1; f1 = -1
* h2 = 0; f2 = 0
* h3 = -4.863281; f3 = -4.863281
*
*/
编辑:
使用从一半到浮动的投射。