Today, we’ll talk about ASCII and how to utilize it in the C++ language for programming. ASCII stands for “American Standard Code for Information Interchange” and is a standardized information encoding technique that is used for communication between humans and computers electronically. Computers cannot understand the language of humans that is why to work with characters, numbers, punctuation, and other computer-related characters there are already predefined numeric values called ASCII values. Let us just explore how ASCII characters behave and how we may retrieve the value of any computer-related type of character inside the coding language C++ so that we can fully comprehend the notion of ASCII in the field of computer science.
Introduction
In C++ programming language, the variables we declared of character type do not contain any value but they contain ASCII values. Also, every character parameter is allocated a numeric value between 0 and 127 as its ASCII value, which encodes the character variable in numeric form. For instance, if we want to get the value of the ‘D’ character then its ASCII value will be 68.
Syntax:
To get the ASCII value of any input character variable in the C++ programming language, it has an implementation rule so that we can easily access the ASCII value without getting any type of error in the code. First, we will declare the variable of the datatype character where we will store the input character to get the ASCII value. After that, we will write the integer datatype and we will pass the variable name in it which we have already created above. We have declared the integer datatype because we want to display the numeric value of the input character.
Return Value:
In return, we will get the standard numeric value from 0 to 127 of any input character in C++ programming.
Example 01:
To use the ASCII format in C++ programming language, let us implement a practical example of ASCII for understanding the concepts completely. To write a code of ASCII in C++ programming language, we always need an IDE that translates the code into the computer language and shows the desired output to the user. So, open any reasonable IDE to write the code. In C++ programming, we first import the module of the predefined function so that we can easily use the functions in the program.
We have included the “#include <iostream>” module so that we can use the cin() and cout() functions in the program because these functions are the very basic function of any program which is used to get the data from the user and display the data to the user’s console window. Then, we have to write the “using namespace std” so that we cannot assign the same name in the entire context in the existing program.
using namespace std;
int main()
{
cout << "*---Implementation of ASCII Format---*" << endl;
char ch;
cout << "\nEnter any character: ";
cin >> ch;
cout << "ASCII Value of " << ch << " is: " << int(ch);
return 0;
}
After importing the module, let us start the main() function and implement the logic of the program here. In line 7, we have printed the message so that the user can understand what we are going to implement in the program. Next, we declared a character type variable “ch” and have the value of “ch” from the user by using the cin() function and stored the value in the “ch” variable. In line 12, we used the cout() method to print the ASCII value of the character which the user has inserted in “ch” and then we passed the “ch” with an integer datatype so that we get the numeric ASCII value of that character. Let us check what we got after compiling the program.
Case 01:
In this case, we have inserted the “#” symbol to get the ASCII value and in return, we get the 35 values.
Case 02:
This time we stored the right small bracket and we have the 41 ASCII value.
Case 03:
In this instance, the compiler computed the ASCII value 71 for the capital “G” letter and the integer 109 for the lowercase “m” character.
Example 02:
Now, let us begin by describing the ASCII method’s next demonstration in C++ programming. We require any C++ compiler that is compatible with the header files which we are going to use in our program. Basic libraries are usually required when writing C++ programs so that C++ manipulators can be applied to already-written code. The “#include <iostream>” is the first library we use in this implementation. The “#” symbol tells the translator to import the package, the keyword “include” incorporates the header file into the program, and “iostream” implies receiving user input and displaying it.
#include<string.h>
using namespace std;
int main()
{
cout << "*----------Implementation of ASCII Format----------*" << endl << endl;
char character, str[20];
cout << "Enter the String to get the numeric values of each string character: ";
gets(str);
cout << "\nCharacters\tASCII Value\n";
int i = 0, num_val;
while(str[i])
{
character = str[i];
num_val = character;
cout << character<<"\t\t" << num_val << endl;
i++;
}
cout << "\nThe length of input string is: " << strlen(str);
return 0;
}
Next, we have added another header file, referred to as the “#include <string.h>” package, enabling the use of strings and string methods throughout the complete illustration. To preserve classes, functions, and variables from employing identical scopes all across the rest of the program, we then added the “using namespace std” clause. After all of the fundamental packages and commands have been included, we can now go on to the program’s main() method. The actual line of code that we want to write and retrieve outcomes from has been written in the main() function body.
We have declared two-character type variables “character” and “str” and we set the maximum length of “str” as 20. Then, we used the cin() method to take the input string from the user of a maximum length of 20 by using the gets() method and passed the variable “str” in it.
Next, we have declared two more variables of integer type “i” containing 0 value in it and “num_val”. To display the string that the user has stored in “str”, we have used a while loop that prints each character of the string one by one. We set the condition of the while loop which is “str[i]”. In the loop body, we have assigned str[i] to the “character” variable so that the compiler displays the character of the string. Next, we have stored “character” in the “num_val” variable which is an integer type that displays the numeric values of each character on the user’s screen by passing it in the cout() method in line 20.
Then, we used the incremental operator to increment the “i” until the compiler displays all the characters of the string that the user entered. After all the characters have been displayed, the while loop will be terminated and print the length of the string that which user has entered by using the strlen() function of the string. At the end of the program, we will return 0 to the main() function to end the execution of the program.
As you see below in the user console window, the user has entered “LiNuX”. According to the uppercase and lowercase letters, the compiler has displayed the ASCII values of each character. It also displayed the length of the string which is 5.
Conclusion
In this article, we have learned how to use the ASCII format in C++ programming language. First, we discussed ASCII and why we need ASCII values and then we learned the writing rules of the ASCII format. We have implemented different examples of ASCII with different logic so that the user can easily understand the concept.