Convert ASCII Value to char in C++
To convert an ASCII value to a char, we can simply type-cast an integer (ASCII value) to a char type or directly use the char() function, which basically performs the same operation as type casting.
Syntax to Convert ASCII to Char in C++
char charName = (char)asciiValue; //typecasting to char type //or char charName = char(asciiValue); //directly using char() function
C++ Program to Convert ASCII to Char
The below program demonstrates how we can convert an ASCII value to a char in C++.
// C++ program to convert ASCII to char
#include <iostream>
using namespace std;
int main()
{
// Define ASCII values for characters 'A' and 'B'.
int asciiValue1 = 65;
int asciiValue2 = 66;
// Typecast the ASCII values to char type
char character1 = (char)asciiValue1;
cout << "The character for ASCII value " << asciiValue1
<< " is " << character1 << endl;
// use char() function tfor conversion
char character2 = char(asciiValue2);
cout << "The character for ASCII value " << asciiValue2
<< " is " << character2 << endl;
return 0;
}
Output
The character for ASCII value 65 is A The character for ASCII value 66 is B
Time Complexity: O(1)
Auxilliary Space: O(1)
How to Convert ASCII Value into char in C++?
In C++, ASCII (American Standard Code for Information Interchange) values are numerical representations of characters that are used in the C++. In this article, we will learn how to convert an ASCII value to a char in C++.
Example:
Input: int asciiValue = 65; Output: Character for ASCII value 65 is: 'A'
Contact Us