Reputation: 137
I'm beginner in c++ and I want to write a program that prints the ASCII value of the alphabet ('a', 'b',.... 'z') and of the numbers ('0'...'9').
I'm able to print the ASCII value of the alphabet using the "int()" function.
Example:
char a = 'a';
std::cout<<int(a);
The result of the code above was "97", as I expected.
But, I don't know how to print the ASCII value of the numbers. For example, if I have the int 2, I want to get the ASCII value of 2, that it's 48.
I've tried to use the function "int()" but the result it's the int value, not the ASCII value.
Upvotes: 2
Views: 12883
Reputation: 206577
For example, if I have the int 2, I want to get the ASCII value of 2, that it's 48.
std::cout << (2 + '0');
will print the ASCII value of 2
.
If a variable a
is known to be 0
- 9
, you can use:
std::cout << (a + '0');
to print the ASCII value corresponding to a
.
Upvotes: 3