• Forums
• Posts
Latest Posts
Active Posts
Recently Visited
Search Results
• Page Extras
• Forum Themes
• AVR Freaks

### Hot!Converting ASCII to FLOAT and FLOAT TO ASCII

Author
Guest
Super Member
• Total Posts : 80502
• Reward points : 0
• Joined: 2003/01/01 00:00:00
• Location: 0
• Status: online
5 (1)

# Converting ASCII to FLOAT and FLOAT TO ASCII

I would like to know , how it is possible to do a conversion ItoF and FtoI in C.Could you give me an example of these conversion.

My tools are : MPLAB ver 5.60 and MCC18 1.10

My PIC is a 18F452

Eric CALLUT

email : eric.callut@ulg.ac.be

Nickolays
New Member
• Total Posts : 1
• Reward points : 0
• Joined: 2020/05/01 03:28:25
• Location: 0
• Status: offline
Re: Converting ASCII to FLOAT and FLOAT TO ASCII 2020/05/01 19:55:37 (permalink)
0
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[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';
}
}
ric
Super Member
• Total Posts : 27934
• Reward points : 0
• Joined: 2003/11/07 12:41:26
• Location: Australia, Melbourne
• Status: online
Re: Converting ASCII to FLOAT and FLOAT TO ASCII 2020/05/05 17:09:53 (permalink)
+2 (2)
I really don't think this OP is waiting for an answer nearly 18 years later...

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
jtemples
عُضْوٌ جَدِيد
• Total Posts : 11928
• Reward points : 0
• Joined: 2004/02/13 12:31:19
• Location: Southern California
• Status: offline
Re: Converting ASCII to FLOAT and FLOAT TO ASCII 2020/05/05 17:19:34 (permalink)
+2 (2)
18 years later...

Is that a record?
nigelwright7557
Super Member
• Total Posts : 446
• Reward points : 0
• Joined: 2006/11/06 08:15:51
• Location: 0
• Status: offline
Re: Converting ASCII to FLOAT and FLOAT TO ASCII 2020/05/05 17:52:32 (permalink)
0
Better late than never lol

Antipodean
Super Member
• Total Posts : 1870
• Reward points : 0
• Joined: 2008/12/09 10:19:08
• Location: Didcot, United Kingdom
• Status: offline
Re: Converting ASCII to FLOAT and FLOAT TO ASCII 2020/05/05 18:11:33 (permalink)
+1 (1)
nigelwright7557
Better late than never lol

and only one half of the answer as well ...

Alan
PStechPaul
Super Member
• Total Posts : 2807
• Reward points : 0
• Joined: 2006/06/27 16:11:32
• Location: Cockeysville, MD, USA
• Status: online
Re: Converting ASCII to FLOAT and FLOAT TO ASCII 2020/05/05 18:13:20 (permalink)
-1 (1)
It's also a "guest" account. ;)

jtemples
عُضْوٌ جَدِيد
• Total Posts : 11928
• Reward points : 0
• Joined: 2004/02/13 12:31:19
• Location: Southern California
• Status: offline
Re: Converting ASCII to FLOAT and FLOAT TO ASCII 2020/05/05 18:25:50 (permalink)
+1 (1)
...and it relies on non-standard functions (itoa) that aren't included...