String to float conversion?

It's my third question today, and i hope it will be answered as quickly as the other two! (you guys are awesome!)
And after this my project is done, so no more questions for a while! :smiley:

ok so i read a string off twitter: the temperature i set bla bla bla you dont care
all you care about is that i get this string (called 'tweet'): ">22" and that i nead this float(called f): "22"

How can i do this? My problem is the '>' or this code: would work (?)

          char carray[tweet.length() + 1]; //determine size of the array
          tweet.toCharArray(carray, sizeof(carray)); //put readStringinto an array
          float f = atof(carray); //convert the array into an Integer 
          desiredT = f;

What i get is '0.00', the standard error you get from atof when the string isn't 'clean' i believe...

Thank youuuuuuu!

Try this piece of code:

if (carray[0] == '>') {  // does the string begin with a ">" symbol?
  carray[0] = ' ';       // replace with a space if it does

Or just convert the second character onwards:

float f = atof(carray+1);


float f = atof(carray+1);

carray is a pointer to the beginning of array of characters, carray+1 is the pointer to the second character in that array, i.e. the one after the ">".

Also is there any reason you are using the String class? Why not write directly into your character array?

i used

float f = atof(carray+1);

and my code now works perfectly! a 4 month mission is complete, thanks!
odometer, your solution works as well, thank you :wink:

the reason there is a string is because i'm reading the value 22 from twitter (i'm making, i made now actually, a twitter controlled thermostat). The twitter reading code gives me a string with the > in it and i need the 22 as the desired temperature for the apartment :slight_smile: makes sense? there's probably millions of ways to make the whole thing work in an easier way, but it's my first try :wink:

a happy E