Re: Converting ASCII to FLOAT and FLOAT TO ASCII
2020/05/01 19:55:37
(permalink)
This is an better ftos:

void ftos(float f, unsigned char size, unsigned char *str)

{

unsigned char pos=0; // position in string

unsigned char len=0; // length of decimal part of result

unsigned char curr[2]; // temp holder for next digit

int value; // decimal digit(s) to convert

curr[1]='\0';

value = (int)f; // truncate the floating point number

itoa(value,str,10);

if(size!=0)

{

while(str[len]!='\0') len++; // similar to strlen - find out how big the integer part was

// now str array has the digits before the decimal

if(f<=-1) // handle negatives <=-1

{

f *= -1;

value *= -1;

}

pos = len; // position the pointer to the end of the integer part

str[pos++] = '.'; // add decimal point to string

if(f<0 && f>-1) // handle negatives with zero integer digit numbers

{

f *= -1;

value *= -1;

for(pos=0;pos<(len+1);pos++) str[pos+len+1]=str[pos];//move int string right

str[0]='-'; //add -0.

str[1]='0';

str[2]='.';

pos = len+2; // position the pointer to the end of the zero integer part

size++;

}

while(pos<(size+len+1)) // process remaining digits

{

f=f-(float)value; // hack off the whole part of the number

f*=10; // move next digit over

value = (int)f; // get next digit

itoa(value, curr,10); // convert digit to string

str[pos++] = curr[0]; // add digit to result string and increment pointer

}

str[pos]='\0';

}

}