/*将浮点数处理为ASCII码
6位有效数字,这里为了整齐 如:0.01234当作6位有效数字
*/
void float_TO_ascii(float a, uint8_t dat[10])
{
if(1000<=a&&a<10000)
{
dat[0] = (int)a%10000/1000 + 0x30;
dat[1] = (int)a%1000/100 + 0x30;
dat[2] = (int)a%100/10 + 0x30;
dat[3] = (int)a%10 + 0x30;
dat[4] = 0x2e;
dat[5] = (int)(a*10)%10 + 0x30;
dat[6] = (int)(a*100)%10 + 0x30;
dat[7] = 0;
dat[8] = 0;
dat[9] = 0;
}
if(100<=a&&a<1000)
{
dat[0] = (int)a%1000/100 + 0x30;
dat[1] = (int)a%100/10 + 0x30;
dat[2] = (int)a%10 + 0x30;
dat[3] = 0x2e;
dat[4] = (int)(a*10)%10 + 0x30;
dat[5] = (int)(a*100)%10 + 0x30;
dat[6] = (int)(a*1000)%10 + 0x30;
dat[7] = (int)(a*10000)%10 + 0x30;
dat[8] = 0;
dat[9] = 0;
}
if(10<=a&&a<100)
{
dat[0] = (int)a%100/10 + 0x30;
dat[1] = (int)a%10 + 0x30;
dat[2] = 0x2e;
dat[3] = (int)(a*10)%10 + 0x30;
dat[4] = (int)(a*100)%10 + 0x30;
dat[5] = (int)(a*1000)%10 + 0x30;
dat[6] = (int)(a*10000)%10 + 0x30;
dat[7] = (int)(a*100000)%10 + 0x30;
dat[8] = 0;
dat[9] = 0;
}
if(1<=a&&a<10)
{
dat[0] = (int)a%10 + 0x30;
dat[1] = 0x2e;
dat[2] = (int)(a*10)%10 + 0x30;
dat[3] = (int)(a*100)%10 + 0x30;
dat[4] = (int)(a*1000)%10 + 0x30;
dat[5] = (int)(a*10000)%10 + 0x30;
dat[6] = (int)(a*100000)%10 + 0x30;
dat[7] = 0;
dat[8] = 0;
dat[9] = 0;
}
if(0<=a&&a<1)
{
dat[0] = 0x30;
dat[1] = 0x2e;
dat[2] = (int)(a*10)%10 + 0x30;
dat[3] = (int)(a*100)%10 + 0x30;
dat[4] = (int)(a*1000)%10 + 0x30;
dat[5] = (int)(a*10000)%10 + 0x30;
dat[6] = (int)(a*100000)%10 + 0x30;
dat[7] = 0;
dat[8] = 0;
dat[9] = 0;
}
if(-1<a&&a<0)
{
dat[0] = 0x2d;
dat[1] = 0x30;
dat[2] = 0x2e ;
dat[3] = (int)(-a*10)%10 + 0x30;
dat[4] = (int)(-a*100)%10 + 0x30;
dat[5] = (int)(-a*1000)%10 + 0x30;
dat[6] = (int)(-a*10000)%10 + 0x30;
dat[7] = (int)(-a*100000)%10 + 0x30;
dat[8] = 0;
dat[9] = 0;
}
if(-10<a&&a<=-1)
{
dat[0] = 0x2d;
dat[1] = (int)(-a)%10 + 0x30;
dat[2] = 0x2e ;
dat[3] = (int)(-a*10)%10 + 0x30;
dat[4] = (int)(-a*100)%10 + 0x30;
dat[5] = (int)(-a*1000)%10 + 0x30;
dat[6] = (int)(-a*10000)%10 + 0x30;
dat[7] = (int)(-a*100000)%10 + 0x30;
dat[8] = 0;
dat[9] = 0;
}
if(-100<a&&a<=-10)
{
dat[0] = 0x2d;
dat[1] = (int)(-a)%100/10 + 0x30;
dat[2] = (int)(-a)%10 + 0x30;
dat[3] = 0x2e ;
dat[4] = (int)(-a*10)%10 + 0x30;
dat[5] = (int)(-a*100)%10 + 0x30;
dat[6] = (int)(-a*1000)%10 + 0x30;
dat[7] = (int)(-a*10000)%10 + 0x30;
dat[8] = 0;
dat[9] = 0;
}
if(-1000<a&&a<=-100)
{
dat[0] = 0x2d;
dat[1] = (int)(-a)%1000/100 + 0x30;
dat[2] = (int)(-a)%100/10 + 0x30;
dat[3] = (int)(-a)%10 + 0x30;
dat[4] = 0x2e ;
dat[5] = (int)(-a*10)%10 + 0x30;
dat[6] = (int)(-a*100)%10 + 0x30;
dat[7] = (int)(-a*1000)%10 + 0x30;
dat[8] = 0;
dat[8] = 0;
}
if(-10000<a&&a<=-1000)
{
dat[0] = 0x2d;
dat[1] = (int)(-a)%10000/1000 + 0x30;
dat[2] = (int)(-a)%1000/100 + 0x30;
dat[3] = (int)(-a)%100/10 + 0x30;
dat[4] = (int)(-a)%10 + 0x30;
dat[5] = 0x2e ;
dat[6] = (int)(-a*10)%10 + 0x30;
dat[7] = (int)(-a*100)%10 + 0x30;
dat[8] = 0;
dat[9] = 0;
}
}
有木有无聊的同学们,帮忙把空间复杂读缩小下,算法优化下:lol
|