String into Int

hello,

string str = "17462309";
int a;

a = str.at(0);

cout << a; // 49
cout << str.at(0); //1

my question is why is a 49 and not 1?
and i know that i have to add this line

a = (int)str.at(0) - (int)'0'; // 1

but can u explain me what the (int)'0' is doing ?

thank you.
(int)'0' seems to give the position of the character '0' on the ASCII table. If a is an integer type it's assigned a corresponding ASCII value, and for it to represent an integer correctly in a string it needs to compute a difference of ASCII values.

http://www.asciitable.com/
Topic archived. No new replies allowed.