Short answer
To get the numeric code of a char, assign it to an int or cast it with static_cast<int>. To get the number a digit character stands for, subtract '0' from it. On a system that uses ASCII, the letter 'A' becomes 65, and the digit '7' becomes 7 after you subtract '0'.
Two different goals: the code or the digit
A char holds a small integer code for one character. Which int you want depends on what you mean by “convert”:
- The character’s code. The letter
'A'has the code 65 in ASCII. Assigning thecharto anintgives that code. - The digit’s value. The character
'7'stands for the number 7. Subtracting'0'gives that value.
The two results are different. Reading '7' as a code gives 55, not 7.
Getting a char’s numeric code
Assigning a char to an int converts it automatically. The value of any char fits in an int, so nothing is lost. An explicit cast does the same thing and makes the conversion visible to a reader:
#include <iostream>
int main() {
char letter = 'A';
int code = letter; // implicit conversion
int explicitCode = static_cast<int>(letter); // explicit cast
std::cout << code << " " << explicitCode << "\n"; // 65 65 on an ASCII system
}
Getting the number a digit character stands for
For a digit, subtract '0':
char digit = '7';
int value = digit - '0'; // value is 7
This works because the C and C++ standards require the digits '0' through '9' to have consecutive values. So '9' - '0' is 9. Both operands are promoted to int before the subtraction, so value is an int.
Writing '0' reads more clearly than the number 48. The number 48 is the code for '0' only on ASCII systems, while the character literal always means the digit zero.
Checking that a char is a digit first
Subtracting '0' from a letter does not give a useful number. For example, 'a' - '0' is 49 on an ASCII system. Check first with std::isdigit from <cctype>:
#include <cctype>
char c = 'x';
if (std::isdigit(static_cast<unsigned char>(c))) {
int value = c - '0';
}
std::isdigit returns a nonzero value for digit characters. Its argument should be a value that fits in an unsigned char, which is why the cast comes first. Passing a negative value can cause undefined behavior.
Converting a string of digits
A single char holds one character. A number such as 42 is two characters, so it has to be read from a string instead. The approach for whole numbers is covered in c convert string to int.
Common mistakes
- Expecting
'7'to be 7.int x = '7';setsxto 55 on an ASCII system. Subtract'0'to get the digit’s value. - Forgetting the subtraction.
int value = digit;gives the character’s code, not the digit. - Passing a single char to
std::stoi.std::stoitakes astd::string, not achar, so passing onecharwill not compile. - Assuming char is signed. Whether plain
charis signed depends on the compiler. The difference matters for values outside the ASCII range, as explained in signed vs unsigned in c. - Expecting std::to_string to return the letter. Passing a
chartostd::to_stringalso gives its numeric code. If you want to know how that overload is chosen, read What does to_string() do in C++?