How to convert from ASCII chain to a float variable

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to convert from ASCII chain to a float variable

Jump to solution
3,170 Views
PabloA
Contributor II

HI there, I'm working in a logger system that send logged data to a PC. By now I'm developing the PC Software in C# with a lot of very usefull tools that allow me to change from Hexa to Float, Flota to Hexa, to String... what I want, but I know when I want to develope in Visual4Android I will not be so lucky.

So, someone knows how can I convert ASCII data string (4 bytes received by serial SPI) in to a float data? In other words the opposite of sprintf.

Mi idea is send a float data converted by sprinf in ASCII string and reseive float configuration data also in ASCII strings.

I'm using the MCF51CN128 ColdFire.

 

Thank you for your time!

Regards!

 

 

Pablo, from Buenos Aires.

 

Labels (1)
0 Kudos
Reply
1 Solution
1,893 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

try atof()  ----- ascii to floating point numbers

#include <stdlib.h>


void main(void) {

    volatile double f;
    char sf[10]="1.23";
    f = atof(sf);   //atof
}

Hope this helps!

View solution in original post

0 Kudos
Reply
5 Replies
1,894 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

try atof()  ----- ascii to floating point numbers

#include <stdlib.h>


void main(void) {

    volatile double f;
    char sf[10]="1.23";
    f = atof(sf);   //atof
}

Hope this helps!

0 Kudos
Reply
1,893 Views
adt
Contributor III

Hi ZhangJennie,

I am using TWRK60F120M

codewarrior 10.5 &

mqx4.0.2

I have tried conversion of ascii string to float using atof function as you explained but for conversion of 1.23 I am getting it as 2.06 & also I am getting warning for this api as implicit declaration of function.

Please let me know how to solve this.

Thanks.

0 Kudos
Reply
1,893 Views
TomE
Specialist II

> I have tried conversion of ascii string to float using atof function

This is both a basic "Programming in C" question as well as being related to the compiler you're using. You should be asking questions there.

You should also type error messages you don't understand into Google to find everyone else who has had the same problem - and how they solved it.

Tom

0 Kudos
Reply
1,893 Views
PabloA
Contributor II

Thanks to All, I will try it.

0 Kudos
Reply
1,893 Views
JimDon
Senior Contributor III

So might consider sscanf the inverse of sprintf.

0 Kudos
Reply