自己写的将浮点数据转化为字符串的函数怎么在AVR上不对啊。
这几天自己动手拿MEGA16做个简单的4功能计算器,在数据计算完后为double型的,想把这个double转化为字符串,下午自己写了个程序,在TC2.0上运行的挺好,但是拿到ICC里 出来的数字总是不着边际,心急火燎上来发个帖子求助高手。函数如下:
void TrFtoA(double DataF,unsigned char str[],unsigned char n) //双精度数据DataF,目标字符串str[],小数点的位数n最大为8位
{
unsigned long int lengthZ,lengthX,Temp1,Temp2;
double Temp3,Temp4,Data;
unsigned char linshi2,linshi;
short int i,j,sign;
if(DataF < 0)
{
Data = 0 - DataF;
sign = 1;
}
else
{
Data = DataF;
sign = 0;
}
Temp2 = (unsigned long) Data;
for(lengthZ = 1;Temp2 >= 10;lengthZ ++)
Temp2 = Temp2 / 10;
Temp3= Data;
Temp1 = Temp2 = (unsigned long int) Data;
for(lengthX = 0; ;)
{
if(lengthX == n)
break;
Temp4 = (Temp3 * 10);
Temp1 = (Temp4);
if((Temp1 - (Temp2 * 10)) == 0)
break;
else
{
Temp3 = Temp3 * 10;
Temp2 = (unsigned long int)Temp3;
lengthX ++;
}
}
Temp1 = Temp1;
for(i = 0;Temp1 >= 10;i ++)
{
linshi = ((Temp1 % 10) + 48);
Temp1 = Temp1 / 10;
}
j = i;
linshi = Temp1 + 48;
linshi = '\0';
for(i = 0;j >= 0;i ++)
{
linshi2 = linshi;
str = linshi2;
}
str = '\0';
for(i = (lengthZ + lengthX + 2); i >= lengthZ;i --)
{
if(i ==lengthZ)
{
j = str;
str = 0x2E;
}
else
{
str = str;
}
}
if(sign == 1)
{
for(i = (lengthZ + lengthX + 3); i >= 0;i --)
{
if(i == 0)
{
str = 0x2D;
}
else
{
str = str;
}
}
}
}
如果有高手那里有现成的浮点转换为字符串和字符串转化为浮点的函数最好也给发下。 感激不尽啊。。。 ICC支持64bit的double……你开启了么?
ICC有一个函数叫做ftoa,下面是使用方法……
使用前需要包含头文件stdlib.h
char *ftoa(float f, int *status)
converts a floating-point number to the its ASCII representation. It returns a static buffer of approximately 15 chars. If the input is out of range, *status is set to the constant, _FTOA_TOO_LARGE or _FTOA_TOO_SMALL, defined in stdlib.h, and 0 is returned. Otherwise, *status is set to 0 and the char buffer is returned. This version of the ftoa is fast but cannot handle values outside of the range listed. Please contact us if you need a (much) larger version that handles greater ranges.
As with most other C functions with similar prototypes, *status means that you must pass the address of a variable to this function. Do not declare a pointer variable and pass it without initializing its pointer value. 谢谢 傻孩子了,不过这个函数好像有点误差。
页:
[1]