How to convert String to Int

I'm getting an error on str1 on this line:

intScreenH = (int)str1;

I thought I could cast a string to an integer but I guess not!

I read that I should be using a char *str1 but then myFile. line doesn't work. There must be a way to convert a String to an Int?

int get_SD_Value(char *strFilename)
{
	if (SD.exists(strFilename))
	{
		myFile = SD.open(strFilename);
		if (myFile)
		{
			while (myFile.available())
			{
				str1 = myFile.readStringUntil('\n');
				intScreenH = (int)str1;
			}
			myFile.close();
		}
		return intScreenH;
	}
	else
	{
		Serial.print(strFilename);
		Serial.println(" not found.");
		return 0;
	}
}

I do not know what an Int is so I cannot convert to it.

Perhaps I can convert to an int, but in one place there is a request to convert a string to an int, and in another place there is a request to convert a String to an int.

These are different types, and since only a snippet of the program was provided, I am left to guess that str1 is a buffer for a string, not a String.

itoa(...) or snprintf(...) would work.

By the way, I quickly found this by using Bing; Google would have worked just as well.

Of course, the string in str1 must be properly terminated with a 0. I do not know if readStringUntil(...) does this or not. And of course, I do not understand the point of repeatedly getting a string, repeatedly converting to an int, and then throwing everything away except the last int.

Hi,
Did you tried "the toInt() function allows you to convert a String to an integer number. "

This looks likes the best solution but toInt() gives an error: identifier undefined.

itoa() worked to convert it but only on a char *str1 type, and that type won't work on the line

str1 = myFile.readStringUntil('\n');

The line above wants a defined 'String str1 = "";' type, but then I can't convert it an integer type!

This looks likes the best solution but toInt() gives an error: identifier undefined.

Bullshit. You simply did something wrong in the code you did not post.

Hi,

This is the format int number = atoi(str.c_str());

This is how I used it.

int bcd_dec;

String bcd_strm;

bcd_dec = atoi(bcd_strm.c_str());

This is how I used it.

int bcd_dec;

String bcd_strm;

bcd_dec = atoi(bcd_strm.c_str());

So, what value is in the String? What value did atoi() return? How is what it returned different from what you incorrectly assumed it would return?

Hi,
I am converting a number to character by adding 30 and then to string to be display but need the value as integer back for other calculations,

var1 & var2 = digit

bcd_strm = String( char(var1+0x30))+String(char(var2+0x30)) ;
bcd_dec = atoi(bcd_strm.c_str());
It worked for me. I do not know if this is what he looking for.