Problem using function atof()

Hey I am pulling information off the internet and loading it into a string, then attempting to parse out temperature values for a subsequent calculation.

I’ve managed to get the temperature read into a string with this line:

String TempCS = FC.substring(0,FCLOC);

But the following assignment will not compile, even though TempC is declared as a float in the pre-setup assignment block.

float TempC = atof(TempCS);

I get the following error:

error: cannot convert ‘String’ to ‘const char*’ for argument ‘1’ to ‘double atof(const char*)’

Any help converting ‘String’ to ‘const char*’ would be greatly appreciated!

Thanks!

-Kyle

@grimace06, can you try and see following code works?

char szTempCS[13];

TempCS.toCharArray(szTempCS, 13); 

float TempC = atof(szTempCS);

BTW this length 13 is just arbitrary, you may have to change it.

1 Like

That worked GREAT!

Thank you, looks like the String type was incompatable but I wasn't sure what format it needed to be changed to. Thank you very much, krvarma!

2 Likes

Hi @grimace06

I am glad that @krvarma answer worked for you, using C style char arrays!

Another way is the toFloat method in the String class:

float TempC = TempCS.toFloat();
3 Likes

Thanks @bko!, that’s new to me, may be we should update the docs.

@ bko I tried your method as well and it works just fine… always great to learn new methods.

Thanks for your help!

2 Likes