How to convert from ASCII chain to a float variable

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

How to convert from ASCII chain to a float variable

ソリューションへジャンプ
3,526件の閲覧回数
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,249件の閲覧回数
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,250件の閲覧回数
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,249件の閲覧回数
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,249件の閲覧回数
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,249件の閲覧回数
PabloA
Contributor II

Thanks to All, I will try it.

0 件の賞賛
返信
2,249件の閲覧回数
JimDon
Senior Contributor III

So might consider sscanf the inverse of sprintf.

0 件の賞賛
返信