[英]Converting IEEE 754 packed bit stream into float single precision
I have a problem with converting unsigned long to Float single precision Here is my problem 我将unsigned long转换为Float单精度有问题这是我的问题
I need to communicate in floating point in my program, but all my internal variables are unsigned long, So the design we decided was such that everything will remain same,(in unsigned long) and once a float needs to be given to application, i will convert to float and pass it on. 我需要在程序中以浮点数进行通信,但是我所有的内部变量都是无符号长的,因此我们决定的设计是,一切都将保持不变(无符号长),一旦需要将浮点数赋予应用程序,我将转换为float并继续传递。
So all the variables will have a IEEE 754 bit stream according to 因此,根据以下所有变量,将具有IEEE 754位流:
Steps i follow : 我遵循的步骤:
In debugger i see the same bit pattern (in buffers) for steps 1 &2 在调试器中,我看到步骤1和2的位模式相同(在缓冲区中)
In step 3 too, i see the same binary pattern, but the value is not interpreted as a float 在步骤3中,我也看到了相同的二进制模式,但该值未解释为浮点型
Input : 21.125 输入:21.125
Decimal: 1101594624 小数:1101594624
Code: void ApplicationApi(void * DataPtr) { (*(float32*)((void*)DataPtr))= unsignedLong_val; }
Result in application DataPtr * DataPtr = 1.101594624 * e9 应用程序中的结果DataPtr * DataPtr = 1.101594624 * e9
Am i missing something here or the type case worked ? 我在这里缺少什么吗?
A standard C way to reinterpret the bytes of an unsigned long
as a float
is: unsigned long
float
,重新解释unsigned long
字节的标准C方法是:
y = (union { unsigned long u; float f; }) { x } .f;
This code defines a compound literal that is a union, and it initializes the union with the value of x
, which should be an unsigned long
. 此代码定义了一个作为联合的复合文字,并使用
x
的值初始化该联合,该值应为unsigned long
。 Then it accesses the f
member of the union, which is a float
. 然后,它访问并集的
f
成员,它是一个float
。 This float
value is assigned to y
, which should be a float
. 该
float
值分配给y
,它应该是float
。
Thus, assuming the declaration of ApplicationApi
must remain the same and that the void *DataPtr
it is passed is the result of converting a pointer to a float
to a pointer to void
, its definition could be: 因此,假设
ApplicationApi
的声明必须保持相同,并且传递的void *DataPtr
是将指向float
的指针转换为指向void
的指针的结果,则其定义可以是:
void ApplicationApi(void *DataPtr)
{
* (float32 *) DataPtr =(union { unsigned long u; float32 f; }) { unsignedLong_val } .f;
}
Notes about this code: 关于此代码的注释:
unsigned long
and float32
have the same size. unsigned long
和float32
具有相同的大小。 unsignedLong_val
to have file scope. unsignedLong_val
具有文件范围是不希望的。 It would be preferable to pass it as a parameter. void *
. void *
传递。 It could be passed as a float32 *
. float32 *
传递。 Notes about code leading up to this code: 有关导致该代码的代码的注释:
unsigned long
may have endian issues. unsigned long
汇编的方式可能存在字节序问题。 unsigned long
could have trap values that prevent it from being used to hold arbitrary bytes. unsigned long
可能具有陷阱值,从而无法将其用于保存任意字节。 About the C standard and reinterpreting bytes: 关于C标准并重新解释字节:
.
.
operator and a member name has the value of the named member. memcpy
is sometimes used for this.) memcpy
。) Try with this cast : 尝试使用此演员表:
void ApplicationApi(void * DataPtr)
{
*(float32*)DataPtr = *(float32*)unsignedLong_val;
}
You have to declare that the right value is a float value. 您必须声明正确的值是浮点值。
With your cast, the right value is an integer and the left value a float. 对于您的类型转换,右边的值是一个整数,左边的值是一个浮点数。 The compiler convert implicitly the integer to a float value.
编译器将整数隐式转换为浮点值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.