简体   繁体   English

2字节解压为4字节浮点数

[英]Decompression of 2 byte to 4 byte float

I have some animation data (x,y,z), which is represented as 2 byte structures and written in Little Endian.我有一些动画数据 (x,y,z),它表示为 2 字节结构并用 Little Endian 编写。 I know that they should be a 4 byte floating point, so i have to unpack them.我知道它们应该是一个 4 字节的浮点数,所以我必须解压它们。 I collected a few sample values as precise as it was possible (they doesn't represent exactly packed values, but very close to them) and roughly divided packed values on few ranges.我尽可能精确地收集了一些样本值(它们不代表精确的压缩值,但非常接近它们)并在几个范围内粗略地划分压缩值。

Sample values (Little Endian):示例值(小端):

  • 0.048879981 - 0x0046 0.048879981 - 0x0046
  • 0.056879997 - 0x0047 0.056879997 - 0x0047
  • 0.253880024 - 0x0050 0.253880024 - 0x0050
  • 0.313879967 - 0x0051 0.313879967 - 0x0051
  • 0.623880029 - 0x0055 0.623880029 - 0x0055
  • 1.003879905 - 0x0058 1.003879905 - 0x0058
  • -0.066120029 - 0x00С8 -0.066120029 - 0x00С8
  • -0.1561199428 - 0x00СD -0.1561199428 - 0x00СD
  • -0.8691199871 - 0x00D7 -0.8691199871 - 0x00D7

Ranges:范围:

  • 0x0000 : zero 0x0000:
  • [0x0000,0x0014] : invisible changes (increasing probably) [0x0000,0x0014] :不可见的变化(可能增加)
  • [0x0014, ....] : increasing (visible) [0x0014, ....] : 增加(可见)
  • 0x0080 : zero , probably the point of sign change 0x0080 :,可能是符号变化的点
  • [0x0080,0x00B0] : invisible changes (decreasing probably) [0x0080,0x00B0] :不可见的变化(可能减少)
  • [0x00B0, ....] : decreasing (visible) [0x00B0, ....] : 递减(可见)

There are gaps (....) on the ends of ranges because it is hard to check them correctly, but i assume such big values which are lying close to these ends doesn't used in practice.范围的末端有间隙(....),因为很难正确检查它们,但我认为靠近这些末端的如此大的值在实践中不会使用。

Also, it looks like a symmetry between positive and negative ranges, for example i tested 0x0058 which gave 1.003879905 and 0x00D8 which gave value close to -1.003879905 but not precise.此外,它看起来像正负范围之间的对称性,例如我测试了0x0058 ,它给出了1.0038799050x00D8 ,它给出的值接近-1.003879905不精确 Maybe it happened because of slightly offset observed after 0x0080 , when visible decreasing starts from 0x00B0 , but it should be about 0x0094 if entire range had equal symmetry.可能是因为在0x0080之后观察到的轻微偏移而发生的,当可见减少从0x00B0开始时,但如果整个范围具有相等的对称性,它应该约为0x0094 But slight measure inaccuracy might be as well.但轻微的测量不准确也可能是。

So, how to get a function in C#, that will convert source data to 4 byte floating point?那么,如何在 C# 中获取一个函数,将源数据转换为 4 字节浮点数?

Some initial comments based on the information in the question so far:到目前为止,基于问题中的信息的一些初步评论:

  • byte[] buffer = new byte[4]; is a bad approach because it addresses bytes individually while the other code manipulates bits using shifts within words, and C# does not define endianness.是一种糟糕的方法,因为它单独处理字节,而其他代码使用字内的移位来操作位,并且 C# 没有定义字节序。 Simply use an unsigned 32-bit integer for all the work.只需使用一个无符号的 32 位整数即可完成所有工作。 The code will actually be simpler.代码实际上会更简单。
  • The code does not handle subnormal values properly.该代码未正确处理次正常值。 If num2 is zero and num3 is not zero, the significand ( num3 ) must be shifted and the exponent ( num2 ) must be adjusted.如果num2为零且num3不为零,则必须移动有效数 ( num3 ) 并且必须调整指数 ( num2 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM