Convert ASCII To Char In C++ - Java2Blog
Maybe your like
Table of Contents
- 1. Introduction
- 2. Using the static_cast Operator
- 3. Using Function-Style Casting
- 4. Using C-Style Cast
- 5. Using the Assignment Operator
- 6. Using sprintf() Function
- 7. Conclusion
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.
Using static_cast #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; }| 1234567891011121314 | #include <iostream>using namespacestd; intmain(){intasciiValue;cout<<"Enter ASCII: \n";cin>>asciiValue;charch=static_cast<char>(asciiValue);cout<<"Corresponding character is: "<<ch<<endl;return0;} |
| 12345678 | stark@stark:~/eclipse-workspace/Aditya/src$g++Ascii2Char.ccstark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:65Corresponding 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.
#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; }| 1234567891011121314 | #include <iostream>using namespacestd; intmain(){intascii;cout<<"Enter ASCII: \n";cin>>ascii;charch=char(ascii);cout<<"Corresponding character is: "<<ch<<endl;return0;} |
The output of the above code is given as follows.
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: �| 12345678910111213141516 | stark@stark:~/eclipse-workspace/Aditya/src$g++Ascii2Char.ccstark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:48Corresponding character is:0stark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:97Corresponding character is:astark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:156Corresponding 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.
#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; }| 12345678910111213141516 | #include <iostream>using namespacestd; intmain(){intascii;cout<<"Enter ASCII: \n";cin>>ascii;charch=(char)ascii;cout<<"Corresponding character is: "<<ch<<endl;return0;} |
The output of the code is given below. We can see the output is the same as the output in the previous approach.
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: �| 123456789101112131415 | stark@stark:~/eclipse-workspace/Aditya/src$g++Ascii2Char.ccstark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:97Corresponding character is:astark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:48Corresponding character is:0stark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:158Corresponding 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 string to Char Array in C++
Read more →Remove Character from String in C++
Read more →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.
#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; }| 123456789101112131415 | #include <iostream>using namespacestd; intmain(){intascii;cout<<"Enter ASCII: \n";cin>>ascii;charch=ascii;cout<<"Corresponding character is: "<<ch<<endl;return0;} |
The output of the above code is given below.
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: �| 12345678910111213141516 | stark@stark:~/eclipse-workspace/Aditya/src$g++Ascii2Char.ccstark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:97Corresponding character is:astark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:48Corresponding character is:0stark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:158Corresponding 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.
#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; }| 1234567891011121314151617 | #include <iostream>using namespacestd; intmain(){intascii;cout<<"Enter ASCII: \n";cin>>ascii;char*ch;sprintf(ch,"%c",ascii);cout<<"Corresponding character is: "<<(*ch)<<endl;return0;} |
The output of the code given above is given below. You can see the output is the same and perfect as our given input.
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: �| 123456789101112131415 | stark@stark:~/eclipse-workspace/Aditya/src$g++Ascii2Char.ccstark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:97Corresponding character is:astark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:48Corresponding character is:0stark@stark:~/eclipse-workspace/Aditya/src$./a.outEnter ASCII:158Corresponding 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.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve. Yes NoRelated posts:
- Stack implementation in C++
- Convert string to Char Array in C++
- Remove Character from String in C++
- Get Number of Elements in Array in C++
- Convert Vector to Array in C++
- Print Vector in C++
- How to Initialize an Array in Constructor in C++
- Remove Last Element from Vector in C++
- Escape Percent Sign in printf Method in C++
- Count Decimal Places in C++
Tag » How To Get Ascii Value Of Char In C++
-
Get ASCII Value Of Char In C++ | Delft Stack
-
C++ Program To Find ASCII Value Of A Character - Programiz
-
Program To Print ASCII Value Of A Character - GeeksforGeeks
-
C++ Program To Print ASCII Value Of All Characters In The String
-
How To Convert An ASCII Char To Its ASCII Int Value? - Stack Overflow
-
ASCII Chart
-
How Do You Find The ASCII Value Of A Character? - MakeUseOf
-
C++ Program To Find ASCII Value Of A Character - Tutorialspoint
-
ASCII Value Of Character In C++ | Programming - PrepInsta
-
Print Character Through ASCII Value Using Cout In C++ - IncludeHelp
-
C++ Program To Find ASCII Value Of Character - Learn ETutorials
-
Program To Print ASCII Value Of A Character - STechies
-
ASCII Character Set
-
Ascii Value Of A Char In Cpp Code Example - Grepper