//—将单精度浮点转化成unsigned char数组,然后将unsigned char数组转化成ascii码字符串
float BallData[8] = {-8.925133};
unsigned char *f2char;
unsigned char ControlData[64];
void float2uchar2ascii(void)
{
unsigned char i = 0;
f2char = &BallData;//float to unsiged char[]
//8个floatd对应于32个unsigned char,所以f2char为32位
//f2char[32]={0xC10EDC58,0xC10EDC58,0xC10EDC58,0xC10EDC58,…,0xC10EDC58}
for(i=0;i<32;i++)
{
ControlData[2*i ] = f2char[i]/16+(f2char[i]/16>9?55:48);//unsigned char to ascii char
ControlData[2*i+1] = f2char[i]+(f2char[i]>9?55:48);
//本例中,f2char[0]=0xC1,要把它转化为ascii码字符串首先要得到高四位,然后得到低四位
//假如四位的值大于9,则应转化为字母a~f,不然应转化为0~9
//字母的应该在成果上+55,由于a的ascii码为65,基数为10,所以加55
//数字的应该在成果上+48,由于0的ascii码为48,基数为00,所以加48
}
}
//—将ascii码字符串转化成单精度浮点
unsigned char ControlData[64]={“C10EDC58”};
float BallData[8];
void asciistring2single(void)
{
unsigned char i=0,k=0;
unsigned char temp[4];
for(i=0;i<64;i++)
{
temp[i]=ControlData[i%4];
if(i%4==3)
{
BallData[k++]=(*((float*)temp));
}
}
}
//—将16进制与float之间的转化
union f_to_char
{
uchar chr[4];
float ft0;
} test;//界说一个union,直接对test.float操作,即从chr[4]中得到16进制的数组;反之亦然
test.ft0=3.14;//→chr[4]={0x40,0x48,0xF5,0xC3};