Problem with : cannot convert 'String' to 'const char*' for argument '1' ...

I'm having trouble with those few lines. I'm pasting the whole function because i saw that people love to understand the circumnstances of the errors before of giving an answer. Long story short : i'm having trouble with these 2 lines :

temp_string=bluetooth_Buffer.substring(9,13);
temp=atoi(temp_string);

temp_string is just a normal String variable and the arduino IDE come up to me with this error :
"exit status 1
cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)' "

I honestly don't understand why because the .substring method should give me a string which should be used by atoi, soo as far as i know there shouldn't be problem with that. Soo that's why I'm here. Thank you for you time in advance.

void bluetooth_Parser()
{
if (int(bluetooth_Buffer.length())!=13) //Hard coded dimensions and particularities of the message
bluetooth_Buffer = "";
else if (bluetooth_Buffer[0]!='[')
bluetooth_Buffer = "";
else if (bluetooth_Buffer[3]!=',')
bluetooth_Buffer = "";
else if (bluetooth_Buffer[8]!=',')
bluetooth_Buffer = "";
else if (bluetooth_Buffer[13]!=']')
bluetooth_Buffer = "";
else
{ String temp_string, temp_string_debug;
long temp=0;
//Processing the activity figures
temp_string=bluetooth_Buffer.substring(1,3);
temp=atoi(temp_string);
function_temp.activity = int(temp) ;
//Processing the extra_value figures
temp_string=bluetooth_Buffer.substring(4,8);
temp=atoi(temp_string);
function_temp.extra_value = int(temp);
//Processing the extra_value2 figures
temp_string=bluetooth_Buffer.substring(9,13);
temp=atoi(temp_string);
function_temp.extra_value2 = int(temp);

activity_manager();
}

}

For everyone who's interested I found my answer on this : [RISOLTO] ARDUINO - Conversione stringa in numero (Intero) - Software - Arduino Forum

Is actually in italian but what it says is that :
"Gli oggetti string non si possono convertire con atoi e similari, devono essere array di char, per convertire uno string in char basta usare .c_str() "

"String object cannot be converted with atoi and similar, they must be array of char, to convert a string in char you to use .c_str()"

The compiler is not giving me the error anymore since i'm using this writing :

temp=atoi(temp_string.c_str());

I still have to figure out if it totally work, I'll check.

2 Likes

I still have to figure out if it totally work, I'll check.

The String class has a (poorly named) toInt() method that will convert the String to an int.

1 Like