+ 1
How to convert a character of a string to an integer by using stoi()
Hi , I've forgotten the syntax , plz help me to remember that .
9 Answers
+ 3
No need to create a string to convert a char to int. You can directly use binary operators on char as char actually stores the ordinal value of the character as an 8-bit integer
Example:
https://code.sololearn.com/cBHK67ia31da/?ref=app
Also, whenever you need examples or information about any function/class/header, just see the docs. That's always the best thing to do
http://www.cplusplus.com/reference/string/stoi/
https://en.cppreference.com/w/cpp/string/basic_string/stol
Tapabrata Banerjee
I know it's just an example, but just in case you weren't aware, instead of
```
string ch = "";
ch += str.at(1);
int x = stoi(ch);
```
You could simply do
`int x = stoi(str.substr(0, 1));`
+ 1
Tapabrata Banerjee thanks for your help , but this does not solve my problem .
I actually need to convert just one character . Like this int b = stoi(str[0]) . But this syntax is wrong .
+ 1
I do know how to convert a complete string to an integer..
+ 1
Tapabrata Banerjee that's right gentleman , thank you so much. Now I think there are different ways to do that . As I expected another way to do it and you did it in a different way and different syntax .
+ 1
XXX Good.đđ that's a nice to convert a char to an int too.. . Before I had made this discuss I searched for it over and over and I got nothing.. and I agree with you that the best way to find out more , is to read the docs you mention .