所以我有这个二进制文件存储我需要读入数组的半浮点值。
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#不会直接处理半浮动。
答案 0 :(得分:1)
检查以下库,它实现了一个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
*
*/
编辑:
使用来自Half的cast来浮动。