Convert Single Char to Int in C++



In C++, converting a single character into an integer is possible using various techniques like STL functions say atoi() and stoi(). Also, you can solve this with the help of ASCII value or stringstream function.

Converting Single Char into Int

Here, we have list down of four approaches that help to convert the single character into an integer form as follows:

Using ASCII Subtraction

ASCII subtraction converts a digit character to an integer by subtracting '0' from it. This works because digit characters are stored sequentially in ASCII, so '5' - '0' gives 5.

For better understanding, each character has an ASCII value. For eg.:

'0' - 48
'1' - 49
'8' - 56

Example

In the below program '5' - '0' converts the character '5' to the integer 5 by subtracting the ASCII value of '0' (which is 48) from the ASCII value of '5' (which is 53).

#include <iostream>
using namespace std;

int main() {
   char ch = '5';
   int num = ch - '0';
   cout << "Integer: " << num << endl;
   return 0;
}

Output

The above program obtained the following result:

Integer: 5

Using stoi() Function

In C++, stoi() is the STL function that converts a single character into an integer form.

Example

In this example, the stoi() function accepts the parameter as a string in which it also accepts two parameters 1 and ch to convert it into an integer form.

#include <iostream>
#include <string>
using namespace std;

int main() {
   char ch = '7';
   int num = stoi(string(1, ch));
   cout << "Integer: " << num << endl;
   return 0;
}

Output

The above program obtained the following result:

Integer: 7

Using atoi() Function

In C++, atoi() stands for ASCII to integer. Here, atoi() function accepts the parameter of the given string in which input was assigned in the form array by putting the values as '4' and '/0' (null-terminated array). When atoi() works it converts the single char into int.

Example

Following is the illustration of the atoi() function to convert a single character into an integer.

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
   // Null-terminated char array
   char str[] = {'4', '\0'};  
   int num = atoi(str);
   cout << "Integer: " << num << endl;
   return 0;
}

Output

The above program obtained the following result:

Integer: 4

Using stringstream Function

In this approach, use the sstream header which has a stringstream built-in class to set the object and on using the same object it converts the single character into an integer as a result.

Example

In this example, we show the usage of stringstream to convert the single char into an int.

#include <iostream>
#include <sstream>
using namespace std;

int main() {
   char ch = '8';
   int num;
   stringstream ss;
   ss << ch;
   ss >> num;
   cout << "Integer: " << num << endl;
   return 0;
}

Output

The above program obtained the following result:

Integer: 8
Updated on: 2025-04-21T18:07:05+05:30

689 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements