Table of Contents
1. Introduction
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 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.
In this article, we will learn how to convert ASCII value to char in C++. Further, we will understand how each method works and we will learn to use each of the above methods in our code.
2. Using the static_cast Operator
The static_cast
operator is a type casting method that can be used for the conversion of ASCII values to characters. This method is type-safe, ensuring that the conversion is valid at compile-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; int main() { int asciiValue; cout<<"Enter ASCII: \n"; cin>>asciiValue; char ch = static_cast<char>(asciiValue); cout<<"Corresponding character is: "<<ch<<endl; return 0; } |
1 2 3 4 5 6 7 8 |
stark@stark:~/eclipse-workspace/Aditya/src$ g++ Ascii2Char.cc stark@stark:~/eclipse-workspace/Aditya/src$ ./a.out Enter ASCII: 65 Corresponding character is: A |
In this example, the integer asciiValue
holding the ASCII value 65 is converted to the character ‘A’ using static_cast<char>()
. The char
keyword specifies the target type for the conversion.
3. Using Function-Style Casting
Function-style casting is similar to using constructors in object-oriented programming. It’s a direct and readable way to perform the conversion.
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: � |
We can see that the output aligns well with the input we have provided to the code. It’s important to note that when we provide an integer without a corresponding character, a garbage value is printed, as observed in our last output.
4. Using C-Style Cast
We have already seen above that the function style char()
cast can convert an ASCII value to the corresponding integer. However, this is not the only method C++ provides us. C++ also supports old C-style casts, with which we might already be familiar. In this method, we take an integer and explicitly cast it to the char
type using the char
keyword. Then, we assign the character to a variable of type char
. By doing this, we can convert our ASCII value to its corresponding character in C++, as demonstrated 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. We 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:
5. 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. Let’s 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++.
6. 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: � |
It is important to note 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.
7. Conclusion
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.