浮点数在存储时,是存储连续的字节中的,只要设法找到存储位置,就可以得到这些数了。可以定义一个void 的指针,将此指针指向需要存储的浮点数,然后将此指针强制转化为 char 型,这样,利用指针就可以得到组成该浮点数的各个字节的值了。具体程序如下:
#define uchar unsigned char
#define uint unsigned intvoid FtoC(void)
{ float a;
uchar i,*px
uchar x[4]; /*定义字符数组,准备存储浮点数的四个字节*、
void *pf;
px=x; /*px指针指向数组 x*/
pf=&a; /*void 型指针指向浮点数首地址*/
a=34.526;
for(i=0;i<4;i++)
{ *(px+i)=*((char *)pf+i); /*强制void 型指针转成 char 型,因为*/
} /*void 型指针不能运算*/
}
如果已将数存入 EEPROM,要将其取出合并,方法也是一样,可参考下面的程序。
#define uchar unsigned char#define uint unsigned int
void CtoF(void)
{ float a;
uchar i,*px 
 
uchar x[4]={56,180,150,73}; 
 
void *pf; 
 
px=x; 
 
pf=&a; 
 
for(i=0;i<4;i++) 
 
{ *((char *)pf+i)=*(px+i); 
 
} 
 
} 


 会员中心
 会员中心 会员注册
 会员注册 返回顶部
返回顶部 刷新页面
刷新页面 下到页底
下到页底