DEV Community

James
James

Posted on • Originally published at kodelogs.com

How to get the ASCII value of a character in C++?

Have you ever wondered how does the computer understands what key we have pressed on the keyboard? The computer cannot understand the human language i.e. English or any other language. It just understands the numerical language, so every character on the keyboard has a numerical code behind it which is called ASCII code. In this article, we are going to see how to find the ASCII code of a character in C++ but before that, let’s see what an ASCII code is.

ASCII Code:
ASCII stands for American Standard Code for Information Interchange. It is an encoding standard for electronic communication and represents text in computers and other electronic devices. This code is used to represent information in computers as computers can only understand the numerical language. Whenever you press a key on the keyboard, the ASCII code associated with that key is sent to the CPU which is then converted to the binary representation, and operations are performed on it.

Now that we understand what an ASCII code is, let’s move to the program to find the ASCII code of a character in C++:

Find ASCII Value of a Character in C++:
To find the ASCII value of a character, we just need to convert the character to an integer using explicit typecasting. Type Casting refers to converting one type of data to another. The syntax for converting a character to an integer is as follows:

int variable = (int)character;
Program:

include

include

using namespace std;

int main()
{
char ch = 'a';
int ASCII = (int)ch;

cout<<"Character: "<<ch<<endl<<"ASCII Code: "<<ASCII;

return 0;
Enter fullscreen mode Exit fullscreen mode

}
Output:

Character: a
ASCII Code: 97
Image description

Top comments (0)