char to int cpp code example

Example 1: convert char to int c++

char a = '6'; //bounded by 0 and 9
int n1 = a - '0'; 
int n2 = a - 48; 
int n3 = std::stoi(&a);

Example 2: char to int c++

int x = (int)character - 48;

Example 3: char* to int in cpp

int x = std::stoi("42")

Example 4: converting char to integer c++

int x  = '9' - 48;
// x now equals 9 as an integer

Example 5: C++ int to char*

std::string s = std::to_string(number);
char const *pchar = s.c_str();  //use char const* as target type

Example 6: convert char to int c++

int x = character - '0'

Tags:

Cpp Example