In this article, we will explore how to use C++-style casting to convert an int to char, along with the differences between the two available casting methods.
Cast int to char Using C++ Style Casting
Here we present a comprehensive guide on two type casting options in C++ for converting int to char using style casting. These options include static_cast and reinterpret_cast. Both functions take ASCII integer values and then convert those integers to ASCII characters.
Note that the value of the integer should be in the range of ASCII values for alphabets (A-Z, a-z). The ASCII range of characters A-Z is (65-90) and a-z (97-122). To read more about the ASCII table click here.
Method 1: Using static_cast
In C++, the static_cast operator is a compile-time cast for implicit conversions between related types. It performs a safe type conversion without checking the validity of the conversion at runtime.
To cast int to char using C++ static_cast operator, follow the below-given steps:
Step 1: First, declare an int variable and assign it a value.
Step 2: Next, declare a char variable and initialize it with the style cast expression using static_cast.
Step 3: Print the value of the char variable to confirm that the cast was successful.
Here is a complete code that demonstrates the above steps:
using namespace std;
int main() {
int myInt = 65;
char myChar = static_cast<char>(myInt);
cout << "My char value: " << myChar << endl;
return 0;
}
The above code demonstrates how to use style casting to convert an int value to a char. In the main function, it initializes an integer variable to 65 which represents the ASCII character A, casts it to a char using the static_cast operator, and assigns it to a char variable. Finally, the compiler prints the value of the char variable of the program using cout.
Output
Method 2: Using reinterpret_cast
Another option to cast int to char using style casting in C++ is by using the reinterpret_cast operator. This operator allows for the conversion of any pointer type to another type and any integral type to any other data type. The steps to use reinterpret_cast are like those for static_cast, but the syntax for the function implementation is different, which is given below:
Here, type-id is the desired type to which the expression will be cast and expression is the value to be cast. The above operator is used to convert a pointer type to another type or to convert an integral type to any other type.
The following example demonstrates the use of the reinterpret_cast operator in C++:
using namespace std;
int main() {
int i;
int myInt[] = {65,67,68,69};
for (i = 0; i <= 3; i++) {
char myChar = * reinterpret_cast < char * > ( & myInt[i]);
cout << "My char value: " << myChar << endl;
}
return 0;
}
The above code declares an integer array myInt[] and initializes it with 4 ASCII integer values. Then for loop is used to loop through all the values of the integer array. The reinterpret_cast casts the address of myInt[] to a pointer to the char and then dereferences the pointer to get the value of the character. Finally, the output of the character is printed using cout.
Output
Bottom Line
Type casting is an important feature of C++ programming that allows you to convert one data type to another. Converting an integer number to its matching ASCII character is a common type-casting requirement. For this conversion, C-Style casting provides two options: static_cast and reinterpret_cast. You can efficiently cast an int to char in C++ by following any of these options.