What is the method to convert a char to an int in C++?

In C++, you can convert a char type to an int type using either a type cast or the standard library function stoi().

  1. Utilize type casting:
char c = '5';
int num = static_cast<int>(c);
  1. parse()
#include <string>

char c = '5';
int num = std::stoi(std::string(1, c));

Here, char types are converted to std::string types and then the stoi() function is used to convert the string to an integer type.

bannerAds