How to convert from ASCII chain to a float variable

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

How to convert from ASCII chain to a float variable

跳至解决方案
3,416 次查看
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.

 

标签 (1)
0 项奖励
回复
1 解答
2,139 次查看
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 项奖励
回复
5 回复数
2,140 次查看
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 项奖励
回复
2,139 次查看
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 项奖励
回复
2,139 次查看
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 项奖励
回复
2,139 次查看
PabloA
Contributor II

Thanks to All, I will try it.

0 项奖励
回复
2,139 次查看
JimDon
Senior Contributor III

So might consider sscanf the inverse of sprintf.

0 项奖励
回复