Table of Contents
The characters are represented using integers in most of the encoding standards. Therefore, we can convert the characters to the integers and integers to the characters. ASCII stands for American Standard Code for Information Interchange. It is a character encoding scheme that we use to represent characters in computer systems. In the ASCII system, different characters are given different integer codes. For example, “A” is represented by 65, ‘0’ is represented by 48, and ‘a’ is represented by 97. Originally there are 128 ASCII characters and we also have special characters included in the ASCII table such as %, $, etc. In this article, we will learn how to convert ASCII value to char in C++.
How to Convert ASCII values to Char in C++?
In C++, we represent characters using ASCII codes. However, the internal ASCII codes are not visible when we store characters into char data type instead we see the character itself.
We can convert the ASCII value to a character in C++ using multiple ways. We will discuss each of these ways one by one in this article.
- We can use the
char()
function cast in C++ to convert ASCII to a character. - We can also use a C-style cast to convert an integer to its corresponding character.
- We can use the assignment operator to convert ASCII code to a character in C++.
- We can use the
sprintf()
function to directly print the character corresponding to the ASCII value in C++.
Further, we will understand how each method works and we will learn to use each of the above methods in our code.
Convert ASCII to Char in C++ Using Char()
Function
In C++ we have a char()
function cast that we can use to convert an ASCII value to its corresponding character. The char()
function cast takes an integer as the input and converts it to a character corresponding to the integer.
The following code demonstrates how we can use the char()
function cast to convert an integer to a character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; int main() { int ascii; cout<<"Enter ASCII: \n"; cin>>ascii; char ch = char(ascii); cout<<"Corresponding character is: "<<ch<<endl; return 0; } |
The output of the above code is given as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
stark@stark:~/eclipse-workspace/Aditya/src$ g++ Ascii2Char.cc stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 48 Corresponding character is: 0 stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 97 Corresponding character is: a stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 156 Corresponding character is: � |
You can see that the output is very much what we expect for the input we have given to the code. You should take note that when we provide an integer that does not have a character corresponding to it a garbage value is printed. We can see this in our last output.
Convert ASCII to Char in C++ Using C-Style Cast
We have already seen above that function style char()
cast can convert an ASCII value to the corresponding integer. But it is not the only method that C++ provides us. C++ also supports old C-style casts that you might be familiar with. In this method, we simply take an integer and cast it to the char type explicitly using the char keyword. After that, we assign the character to a variable of type char. In this way, we can convert our ASCII value to its corresponding character in C++ as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int main() { int ascii; cout<<"Enter ASCII: \n"; cin>>ascii; char ch = (char)ascii; cout<<"Corresponding character is: "<<ch<<endl; return 0; } |
The output of the code is given below. You can see the output is the same as the output in the previous approach.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
stark@stark:~/eclipse-workspace/Aditya/src$ g++ Ascii2Char.cc stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 97 Corresponding character is: a stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 48 Corresponding character is: 0 stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 158 Corresponding character is: � |
As we can see that last output is a garbage character because it corresponds to a value for which character does not exist.
Further reading:
Convert ASCII to Char in C++ Using the Assignment Operator
We have already seen two ways to convert the ASCII value to its corresponding character. Those two ways are very similar but we are going to see a very simple and equally efficient way to do the same task.
In C++ we can use assignment operators to convert an ASCII integer value to its corresponding character. All we have to do is to use the assignment operator to assign an integer value to a variable with the char data type. This will assign a character corresponding to the ASCII integer value to the variable. It makes us able to convert an ASCII value to char without using type casting in C++ as it is handled by the compiler internally. You can see the following code that uses this approach.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> using namespace std; int main() { int ascii; cout<<"Enter ASCII: \n"; cin>>ascii; char ch = ascii; cout<<"Corresponding character is: "<<ch<<endl; return 0; } |
The output of the above code is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
stark@stark:~/eclipse-workspace/Aditya/src$ g++ Ascii2Char.cc stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 97 Corresponding character is: a stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 48 Corresponding character is: 0 stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 158 Corresponding character is: � |
The output of the code is the same as the outputs in the previous approaches. It is because we have given the same input. It shows that this method works well and converts the ASCII integer value to the corresponding character in C++.
Convert ASCII to Character in C++ Using sprintf() Function
In all the ways that we have discussed above, we have to assign the converted ASCII value to a char data type. However, if we want to directly print the character derived from the ASCII integer value, we can do it by using the sprintf()
function. We can also assign the character to a variable using the sprintf()
function.
The C++ language provides us with the sprintf()
function that lets us write the formatted strings and characters to a buffer. We can use this function to convert our integer ASCII value to its corresponding character. The sprintf()
function takes three arguments.
- This first input argument takes a pointer to a buffer where the resultant of the formatted string is stored. Here, we will use a character variable as the buffer.
- The second input argument takes a string denoting the format specifier. We will use “%c” as the format specifier.
- We will pass the integer ASCII value as the third argument.
After execution of the sprintf()
function, the corresponding char value for the input ASCII value will be assigned to the character variable that has been used as the first input argument.
The following code shows how we can use the sprintf()
function in C++ to convert ASCII value to char.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { int ascii; cout<<"Enter ASCII: \n"; cin>>ascii; char *ch; sprintf(ch ,"%c", ascii); cout<<"Corresponding character is: "<<(*ch)<<endl; return 0; } |
The output of the code given above is given below. You can see the output is the same and perfect as our given input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
stark@stark:~/eclipse-workspace/Aditya/src$ g++ Ascii2Char.cc stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 97 Corresponding character is: a stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 48 Corresponding character is: 0 stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 158 Corresponding character is: � |
You should take note of the fact that the sprintf()
function is used to format the strings and characters and we, therefore, make use of this smartly. It is not exclusively provided for converting the ASCII integer value to its corresponding character.
Conclusion
C++ is a popular and powerful programming language that is used worldwide for multiple applications. We can find it being used in embedded systems, the gaming industry, networking applications, etc. And the language does justice with its popularity by providing us tools and techniques to handle all the needs for programming.
In this article, we have witnessed one such useful application of C++ where we can convert an ASCII integer value to its corresponding character in different ways. We have talked about four different ways which are easy and efficient to perform the operation. This operation seems to be simple but it is a widely used and useful operation and we need it frequently when working with characters.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!