string to float

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

string to float

844 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hcanova on Sat Sep 08 12:39:21 MST 2012
Hi

I'm trying to convert an char vector to float but the compiler return an error

my code like this:

 char buffer[2];
 buffer[0]='2';
 buffer[1]='3';
 float a;
 a= atof(buffer);


some one have one idea, whats happening?

thanks a lot

Hcanova
0 Kudos
8 Replies

770 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hcanova on Sun Sep 09 09:20:41 MST 2012

Quote: Zero
Your compiling against Redlib(none) library :eek:

So atof isn't included :o

See: http://support.code-red-tech.com/CodeRedWiki/LibraryVariants

Solution: Change library :)  http://support.code-red-tech.com/CodeRedWiki/SwitchingCLibrary



Hi Zero

Now work's, thank you for your attention

Regards

hcanova
0 Kudos

770 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sun Sep 09 09:10:03 MST 2012
Your compiling against Redlib(none) library :eek:

So atof isn't included :o

See: http://support.code-red-tech.com/CodeRedWiki/LibraryVariants

Solution: Change library :)  http://support.code-red-tech.com/CodeRedWiki/SwitchingCLibrary
0 Kudos

770 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hcanova on Sun Sep 09 08:45:18 MST 2012

Quote: Zero
No, it should be terminated, but atof is stopping conversion at 'wrong' chars

 char buffer[10];
      buffer[0]='2';
      buffer[1]='3';
      buffer[2]='.';
      buffer[3]='5';
      buffer[4]='7';
      buffer[5]='E';
      buffer[6]='-';
      buffer[7]='1';
      buffer[8]='2';
      buffer[9]='[B][COLOR=Red]Z[/COLOR][/B]';
Result: a = 2.35700001e-011

Real 'confusing' strings aren't converted

 char buffer[10];
      buffer[0]='2';
      buffer[1]='3';
      buffer[2]='.';
      buffer[3]='5';
      buffer[4]='7';
      buffer[5]='E';
      buffer[6]='[COLOR=Red][B]t[/B][/COLOR]';
      buffer[7]='1';
      buffer[8]='2';
      buffer[9]='Z';
Result: a = 0

But atof is newer throwing a compiler error



Hi Thanks for your answers,

I included the stdlib already, but I don't know about the nohost or semihost library have difference?

please see my test attached.

Regards,

hcanova
0 Kudos

770 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sun Sep 09 06:20:33 MST 2012

Quote: JohnR
Maybe the string has to be null-terminated?



No, it should be terminated, but atof is stopping conversion at 'wrong' chars

 char buffer[10];
      buffer[0]='2';
      buffer[1]='3';
      buffer[2]='.';
      buffer[3]='5';
      buffer[4]='7';
      buffer[5]='E';
      buffer[6]='-';
      buffer[7]='1';
      buffer[8]='2';
      buffer[9]='[B][COLOR=Red]Z[/COLOR][/B]';
Result: a = 2.35700001e-011

Real 'confusing' strings aren't converted

 char buffer[10];
      buffer[0]='2';
      buffer[1]='3';
      buffer[2]='.';
      buffer[3]='5';
      buffer[4]='7';
      buffer[5]='E';
      buffer[6]='[COLOR=Red][B]t[/B][/COLOR]';
      buffer[7]='1';
      buffer[8]='2';
      buffer[9]='Z';
Result: a = 0

But atof is newer throwing a compiler error
0 Kudos

770 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by JohnR on Sun Sep 09 05:39:23 MST 2012
Maybe the string has to be null-terminated?

From MSDN re atof()

The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character may be the null character ('\0' or L'\0') terminating the string.

Hope this helps.

JohnR.
0 Kudos

770 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Sep 08 13:12:14 MST 2012
This error isn't caused by your code :eek:

Did you include stdlib.h and are you compiling with nohost or semihost library :confused:
0 Kudos

770 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by hcanova on Sat Sep 08 13:06:53 MST 2012
Sorry I forgot

make: *** [test.axf] Error 1



Quote: Zero
Which error :confused:

0 Kudos

770 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Sep 08 13:04:55 MST 2012

Quote: hcanova
I'm trying to convert an char vector to float but the compiler return an error



Which error :confused:
0 Kudos