convert string to int

I need to use a browser to set and change a variable in my arduino pool control. I am using the variable as the set point for the temperature to turn the heat on and off. The water temperature is monitored and stored in a variable as well.

My issue is this. The variable i need to assign a value to is called int setTemp. When i use a form and pass the value of a text box using GET, I index the string the extract the value of the text box, (the temperature i want to set the heat to)

The trouble i am having is that since the setTemp value is being passed in the URL as a string, I can not assign the value to int setTemp since that variable will only take an integer. What i need to do is convert the value first but I can’t seem to do this.

my URL is xxx.xxx.x.xx:88/settemp.htm?setTemp=85

I am able to extract the 85 but it is being interoperated as a string and not an integer so i need a way to convert it.

I have looked up the use of atoi but the examples are quite involved and a little over my newbie head.

Any help?

What is difficult about atoi?

char myCharInt[] = "49";
int myInt = atoi(myCharInt);

You need to check what atoi( ) returns if it gets a defective input.

atoi( ) supposedly causes "undefined behaviour" !

You need to check on your implementation what it does.

To respond to your question, Im not sure what is so difficult other than to say it isn’t working for me.LOL

	String getReq = HTTP_req;

	int tempSetting = getReq.indexOf("setTemp=");


	setTemp =  atoi(getReq.substring(tempSetting)) ;

My compiler error comes back as:
poolv1.ino:181: error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'
Error compiling

There is a difference between string (character array) and String (data type). atoi accepts only the character array type.

so is there any way to do this?

Sure. You can identify the correct part of the string, extract it into a substring, and then convert that substring to a character array, and then use atoi( ).

You can also identify the actually numerical digit chars and convert them to a number yourself.

Try this

String s = HTTP_req ;
int i=s.lastIndexOf('=') ;
i++ ;
int res=0 ;
while ( true )
{
    char c=s.charAt(i) ;
    if ( isdigit(c) )
    {
         res = 10*res + ( c-'0') ;
         i++ ;
    }
    else break ;
}

If your number is right at the end of the url string, you will need to see for yourself what happens when charAt( i ) is at the end of the string. I don't use arduino strings, so I am not sure what it does.

If isdigit( ) doesn't exist on the arduino..

bool isdigit( char c )
{
    if ( c >= '0' && c <= '9' ) return true ;
   return false;
}

needahobby:
so is there any way to do this?

You can access the internal string using the subscript operator, or the c_str method.

String s = "12345";

int num1 = atoi( &s[ 0 ] );

//Or

int num2 = atoi( s.c_str() );

The first method is useful if you need to start the conversion part way through the string ( or you could do c_str() + offset ).