国米preting无符号32位只要C单precision IEEE-754浮点数 [英] Interpreting a 32bit unsigned long as Single Precision IEEE-754 Float in C

查看:159
本文介绍了国米preting无符号32位只要C单precision IEEE-754浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Microchip的,它是基于标准的C编译器编译XC32

I am using the XC32 compiler from Microchip, which is based on the standard C compiler.

我是一个RS485网络上的阅读从一个设备一个32位的值,并在一个无符号长,我Typedef的作为DWORD储存这一点。

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.

typedef DWORD unsigned long;

既然这样,当我强制转换这个数值为float,我得到的价值基本上是它的浮点版本的整数重新presentation,而不是正确的IEEE-754间preTED浮动。

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.

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

下面,dword_value会以HEX格式0x4366C0C4这为十进制将重新presented为1130807492所以这一个浮子类型转换只是给了我1.130807492 * 10 ^ 9或1130807492.0它来通过是不是我想要的。

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.

我想单precision IEEE-754重presentation这会给我的230.75299072265625一个浮点值

I want the single precision IEEE-754 representation which would give me a float value of 230.75299072265625

所以很明显类型转换浮动对我不起作用。我需要一个可以转换这种形式我的方法。我到处都找过了XC32库,我找不到任何东西。

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.

有谁知道predefined方法,适当做这个跨pretation对我?或者是有一些可能建议的方法,我可以写?我试图避免写我自己的code为这一特定的任务,因为我很担心我没有找到一个有效的解决方案,如果C已经有一个函数这一点。

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.

有趣的是,如果我这样做是为了一个char *,该值将重新psented上的char * $ P $正确的230.75:

The interesting thing is, if I do this to a char*, the value is represented on that char* correctly as 230.75:

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

下面印刷random_char_pointer屏幕给了我这么230.75 sprintf的,必须正确处理间preting我。因此我假设有东西在C处已经给我。任何人都可以帮助?

Here printing random_char_pointer to screen gives me 230.75 so the sprintf must be handling the interpreting for me correctly. Therefore I am assuming there is something in C already for me. Can anyone assist?

推荐答案

推荐的方法做这样的东西是使用工会:

The recommended way to do stuff like this is to use a union:

union {
    DWORD w;
    float f;
} wordfloat;

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

这是你想要根据ISO 9899的:2011§6.5.2.3¶3脚注95:

This does what you want as per ISO 9899:2011 §6.5.2.3 ¶3 footnote 95:

一个后缀前pression其次是运算符和标识符指定结构或联合对象的成员。该值是命名成员, 95),是一个左值,如果第一八佰伴pression是一个左值。如果第一八佰伴pression有限定的类型,结果又
  在如此限定的版本指定成员的类型的

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. If the first expression has qualified type, the result has the so-qualified version of the type of the designated member.

95)如果构件用来读取联合对象的内容是不一样的最后用来存储在对象的值的部件,该值的对象重新presentation的适当部分是reinter $ p $如6.2.6所述PTED作为新类型的对象重新presentation(一个过程有时称为类型双关语)。这可能是重新presentation一个陷阱。

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"). This might be a trap representation.

这篇关于国米preting无符号32位只要C单precision IEEE-754浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆