如何将32位整数转换为float [英] How to convert 32bit integer to float

查看:2873
本文介绍了如何将32位整数转换为float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用I2C通信从Sensor读取数据。输出数据是一个字节中的四个,分别是0x43,0xDB,0x8C,0X2E。

所以我成功地分别读取了这些值。当我尝试组合并将其转换为实数。输出值不是我预期的:



从传感器读取的值是:0x43DB8C2E

根据数据表,它是Big Endian对应439.09。

但我得到的输出是29650.



我尝试过:



我已经尝试过数据表中的示例代码,了解如何读取该值。但输出仍然不是我想要的。



I am trying to read data from a Sensor using I2C communication. The output data is four of one byte, which is 0x43, 0xDB, 0x8C, 0X2E respectively.
So i successfully read those value separately. When i try to combine and convert it to real number. The output value is not what i expected:

The value read from the sensor is: 0x43DB8C2E
According to the data sheet, it is Big Endian and corresponding to 439.09.
But what i got for output is 29650.

What I have tried:

I have tried the example code from the data sheet of how to read that value. But the output is still not what i want.

// CO2 concentration
float co2Concentration;
unsigned int tempU32;
// read data is in a buffer. In case of I2C CRCs have been removed // beforehand. Content of the buffer is the following
unsigned char buffer[4];
buffer[0] = 0x43; //  MMSB CO2
buffer[1] = 0xDB; //  MLSB CO2
buffer[2] = 0x8C; //  LMSB CO2
buffer[3] = 0x2E; //  LLSB CO2

// cast 4 bytes to one unsigned 32 bit integer
tempU32 = (unsigned int)((((unsigned int)buffer[0]) << 24) | (((unsigned int)buffer[1]) << 16) | (((unsigned int)buffer[2]) << 8) |
                          ((unsigned int)buffer[3]));

// cast unsigned 32 bit integer to 32 bit float
co2Concentration = *(float*)&tempU32; // co2Concentration = 439.09f





i打印co2Concentration的值,它是29650



i print the value of co2Concentration and it is 29650

推荐答案

解决方案更简单:

The solution is much simpler:
float co2Concentration = 0;
char *buffer = (char*) &co2Concentration;
buffer[0] = 0x43; //  MMSB CO2
buffer[1] = 0xDB; //  MLSB CO2
buffer[2] = 0x8C; //  LMSB CO2
buffer[3] = 0x2E; //  LLSB CO2


尝试使用联合。您可以从以下内容开始:

Try using a union. Here's something you can get started from:
union U {
   int i;
   float f;
   };
U temp;
temp.f = 439.09;
int i = temp.i;



一步一步,看看等于什么。然后玩你的价值观,看看事情是否在一起。如果传感器没有按标准提供浮动值,则必须提供自定义方法。



希望这有帮助!


Step through that and see what i equals. Then play with your values and see if things come together for you. If the sensor is not supplying the float value per standards, you will have to come up with a custom method.

Hope this helps!

为什么要乱搞指针?

只需投出它:

Why are you messing with pointers?
Just cast it:
double co2Concentration;
unsigned int tempU32;
// read data is in a buffer. In case of I2C CRCs have been removed // beforehand. Content of the buffer is the following
unsigned char buffer[4];
buffer[0] = 0x43; //  MMSB CO2
buffer[1] = 0xDB; //  MLSB CO2
buffer[2] = 0x8C; //  LMSB CO2
buffer[3] = 0x2E; //  LLSB CO2

// cast 4 bytes to one unsigned 32 bit integer
tempU32 = (unsigned int)((((unsigned int)buffer[0]) << 24) | (((unsigned int)buffer[1]) << 16) | (((unsigned int)buffer[2]) << 8) |
                          ((unsigned int)buffer[3]));

// cast unsigned 32 bit integer to 32 bit float
co2Concentration = (double)tempU32; // co2Concentration = 439.09f
	printf("%d\n", tempU32);	
	printf("%f\n", co2Concentration);	

输出为:

And the output is:

1138461742
1138461742





(由于显而易见的原因,我转换为双打,因为我的编译器浮点值不准确。)



(I switched to doubles because the value wasn't accurate in a float on my compiler for obvious reasons).


这篇关于如何将32位整数转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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