How To Convert String to Float

I have a situation where I receive via a serial port a "floating point number". I convert the incoming serial stream into a String value.

Now I need to convert the String to a floating point number for use by the Arduino code.

I found this sample code on the forum:

char* strVal = "3.1415";
float fVal = atop(strVal);

my problem is that although the sample compiles cleanly, if I change the first line to be:
char* strVal = IncomingStringValfromSerial;
I get the following compiler error:

cannot convert 'String' to 'char*' in initialization

I feel I must be close but can't figure the last change to make the code work.

I've searched the forums and only found examples of converting float to String.

char* strVal = IncomingStringValfromSerial;
I get the following compiler error:

cannot convert 'String' to 'char*' in initialization

Well, of course you do. There is nothing implicit in the char class or the string class to convert a string to a char array. There is a method, toCharArray(), that WILL convert a String to a char array.

The documentation for the function is wrong. You'll need to look at the String.h file to find the correct way to use it.

atop()? Never heard of it. You got a link to some documentation?

Sorry. atop was a typo. It should have been atof

How would I use toCharArray() in this code?

I looked for String.h in each of the subdirectories of Arduino and could not find a subdirectory for String.

I searched on Google for String.h but am not sure what I am looking for.

I did find this at ЪУЪ ... I found

///////////////////////////////////////////////////////////////////////
// More than 200 string manipulation functions are provided (see the
// "Download String" section) but only few functions are shown here.
///////////////////////////////////////////////////////////////////////

void ensureCapacity(int capacity);
void setLength(int len);
void setCharAt(int where, char ch);

int parseInt(String ss) {return ss.toInteger();}
int parseInt(char *ss)
{String tmpstr(ss); return tmpstr.toInteger();}
long parseLong(String ss) {return ss.parseLong();}
long parseLong(char *ss)
{String tmpstr(ss); return tmpstr.parseLong();}
float floatValue() {return (float) toDouble(); }
double doubleValue() {return toDouble(); }

This (floatValue) sounds like exactly what I want but I can't figure out how to use it in the code.

I also found this web page String Handling: <string.h> with some functions defined within String.h. Does this mean we can use all these functions? I thought we were limited to those listed in Reference on the Arduino site?

I've kept experimenting since I received your post but am still looking for a solution.

You might try something like below. You will need to figure out the dcecimal point handling.

    Serial.println(readstring);  //so you can see the captured string 
    char carray[readstring.length() + 1]; //determine size of the array
    readstring.toCharArray(carray, sizeof(carray)); //put readStringinto an array
    int n = atoi(carray); //convert the array into an Integer

Or float f = atof(carray); to process floats.

Thanks for your suggestions.

At this point I have the logic doing what I wanted. I tried endless combinations and put in debug statements (writing results to an LCD) at each step until I found how to fix things.

My code is probably so convoluted that I am ashamed to post it. Normally I would post any solution I found to help others but I don't think my code would help anyone.

I had spent more than 40 hours trying to solve this before I posted for help. Since I'm retired my time is worth a lot less than if I were working but the frustration has been putting me at the breaking point. I had decided that I would not go to bed today until I had this solved. Thanks to you guys, I will be able to sleep tonight. :-X

This is a perfect example of why arduinos shouldn't be using C. Nobody would ever have guessed any of that. I want to just use a language that gets on and does what I want it to do instead of all this frilly decorative ornamental ostentatious fussy pretentious bureaucratic jurisdictional beadledom that is C. Stop using C.

For my hobby programming, I use Delphi. The reason I like Pascal/Delphi is that it is strongly typed and once you successfully compile a program you are 90% of the way done. All you have to worry about is errors in your program logic. You don't have to worry that something that compiled cleanly will do something completely different than what you thought.

I want to just use a language that gets on and does what I want it to do instead of all this frilly decorative ornamental ostentatious fussy pretentious bureaucratic jurisdictional beadledom that is C.

What you really mean is that you can't clearly define what it is you want to do, so you want the compiler to guess for you.

Yes.