Type Conversions
// string to int
int num = stoi("2");
// int to string
string s = to_string(12345);
// int to char
char itoc(int number) {
return char('0' + number);
}
// char to int
int ctoi(char c){
return c - '0';
}
// get ascii value of character
#include <iostream>
using namespace std;
int getAsciiValue(char c){
return int(c); // <------ you have to use this method
}
int main(int argc, char const *argv[])
{
char char1 = 'a';
char char2 = 'D';
char char3 = '5';
char char4 = '\n';
cout << getAsciiValue(char1) << endl; // 97
cout << getAsciiValue(char2) << endl; // 68
cout << getAsciiValue(char3) << endl; // 53
cout << getAsciiValue(char4) << endl; // 10
return 0;
}
Last updated