简体   繁体   English

用C语言解释32位无符号长单精度IEEE-754浮点数

[英]Interpreting a 32bit unsigned long as Single Precision IEEE-754 Float in C

I am using the XC32 compiler from Microchip, which is based on the standard C compiler. 我使用的是Microchip的XC32编译器,它基于标准的C编译器。

I am reading a 32bit value from a device on a RS485 network and storing this in a unsigned long that I have typedef'ed as DWORD. 我正在从RS485网络上的设备读取32位值并将其存储在unsigned long中,我将其命名为DWORD。

ie

typedef DWORD unsigned long;

As it stands, when I typecast this value to a float, the value I get is basically the floating point version of it's integer representation and not the proper IEEE-754 interpreted float. 就目前而言,当我将此值转换为float时,我得到的值基本上是它的整数表示的浮点版本,而不是正确的IEEE-754解释浮点数。

ie

DWORD dword_value = readValueOnRS485();
float temp = (float)dword_value;

Here, dword_value would come through in HEX format as 0x4366C0C4 which as a decimal would be represented as 1130807492 so the typecasting of this to a float simply gives me 1.130807492*10^9 or 1130807492.0 which is not what I want. 这里,dword_value将以十六进制格式表示为0x4366C0C4,作为十进制将表示为1130807492,因此对浮点数的类型转换只给我1.130807492 * 10 ^ 9或1130807492.0这不是我想要的。

I want the single precision IEEE-754 representation which would give me a float value of 230.75299072265625 我想要单精度IEEE-754表示,它给我一个浮点值230.75299072265625

So clearly typecasting to float doesn't work for me. 显然,对浮动进行类型转换对我来说不起作用。 I need a method that can convert this form me. 我需要一种可以转换此形式的方法。 I have looked all over in the XC32 library and I cannot find anything. 我在XC32库中看了一遍,但找不到任何东西。

Does anyone know of a predefined method that does this interpretation properly for me? 有没有人知道一个预定义的方法,为我正确地做出这种解释? Or is there maybe some suggested method I can write? 或者可能有一些建议的方法我可以写? I am trying to avoid writing my own code for this specific task as I am worried I do not find an efficient solution if C already has a function for this. 我试图避免为这个特定任务编写自己的代码,因为我担心如果C已经有了这个功能,我找不到有效的解决方案。

The interesting thing is, if I do this to a char*, the value is represented on that char* correctly as 230.75: 有趣的是,如果我对char *执行此操作,则该值将在char *上正确表示为230.75:

sprintf(random_char_pointer, "%.2f, dword_value);

Here printing random_char_pointer to screen gives me 230.75 so the sprintf must be handling the interpreting for me correctly. 在这里打印random_char_pointer到屏幕给我230.75所以sprintf必须正确处理解释。 Therefore I am assuming there is something in C already for me. 因此我假设C中已经存在某些东西。 Can anyone assist? 有人可以帮忙吗?

The recommended way to do stuff like this is to use a union: 这样做的推荐方法是使用union:

union {
    DWORD w;
    float f;
} wordfloat;

wordfloat.w = dword_value;
temp = wordfloat.f;

This does what you want as per ISO 9899:2011 §6.5.2.3 ¶3 footnote 95: 根据ISO 9899:2011§6.5.2.3¶3脚注95,您可以按照自己的意愿行事:

A postfix expression followed by the . 后缀表达式后跟. operator and an identifier designates a member of a structure or union object. 运算符和标识符指定结构或联合对象的成员。 The value is that of the named member, 95) and is an lvalue if the first expression is an lvalue. 该值是指定成员的值, 95)如果第一个表达式是左值,则它是左值。 If the first expression has qualified type, the result has the so-qualified version of the type of the designated member. 如果第一个表达式具有限定类型,则结果具有指定成员类型的限定版本。

95) If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called “type punning”). 95)如果用于读取union对象内容的成员与上次用于在对象中存储值的成员不同,则该值的对象表示的适当部分将被重新解释为新对象表示按6.2.6中描述的类型(有时称为“类型双关”的过程)。 This might be a trap representation. 这可能是陷阱表示。

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

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