C++ Program For int to char Conversion
In this article, we will learn how to convert int to char in C++. For this conversion, there are 5 ways as follows:
- Using typecasting.
- Using static_cast.
- Using sprintf().
- Using to_string() and c_str().
- Using stringstream.
Let’s start by discussing each of these methods in detail.
Examples:
Input: N = 65
Output: AInput: N = 97
Output: a
1. Using Typecasting
Method 1:
- Declaration and initialization: To begin, we will declare and initialize our integer with the value to be converted.
- Typecasting: It is a technique for transforming one data type into another. We are typecasting integer N and saving its value in the data type char variable c.
- Print the character: Finally, print the character using cout.
Below is the C++ program to convert int to char using typecasting:
C++
// C++ program to convert // int to char using typecasting #include <iostream> using namespace std; // Driver code int main() { int N = 97; cout << char (N); return 0; } |
a
Method 2:
- Declaration and initialization: To begin, we will declare and initialize our integer with the value to be converted.
- Typecasting: Declare another variable as character c and assign the value of N to the C
- Print the character: Finally, print the character using cout.
Below is the C++ program to convert int to char using typecasting:
C++
// C++ program to convert // int to char using typecasting #include <iostream> using namespace std; // Driver code int main() { int N = 65; char c = N; cout << c; return 0; } |
A
2. Using static_cast
The integer can be converted to a character using the static_cast function. Below is the C++ program to convert int to char using static_cast:
C++
// C++ program to convert // int to char using static_cast #include <iostream> using namespace std; // Driver code int main() { int N = 65; char c = static_cast < char >(N); cout << c; return 0; } |
A
3. Using sprintf()
Allot space for a single int variable that will be converted into a char buffer. It is worth noting that the following example defines the maximum length Max_Digits for integer data. Because the sprintf function sends a char string terminating with 0 bytes to the destination, we add sizeof(char) to get the char buffer length. As a result, we must ensure that enough space is set aside for this buffer.
Below is the C++ program to convert int to char using sprintf():
C++
// C++ program to convert // int to char using sprintf() #include <iostream> using namespace std; #define Max_Digits 10 // Driver code int main() { int N = 1234; char n_char[Max_Digits + sizeof ( char )]; std:: sprintf (n_char, "%d" , N); std:: printf ( "n_char: %s \n" , n_char); return 0; } |
n_char: 1234
4. Using to_string() and c_str()
The to string() function transforms a single integer variable or other data types into a string. The c_str() method converts a string to an array of characters, terminating with a null character.
Below is the C++ program to convert int to char using to_string() and c_str():
C++
// C++ program to convert // int to char using sto_string() // and c_str() #include <iostream> using namespace std; // Driver code int main() { int N = 1234; string t = to_string(N); char const *n_char = t.c_str(); printf ( "n_char: %s \n" , n_char); return 0; } |
n_char: 1234
5. Using stringstream
A stringstream connects a string object to a stream, allowing you to read from it as if it were a stream (like cin). Stringstream requires the inclusion of the sstream header file. The stringstream class comes in handy when processing input.
Below is the C++ program to convert int to char using stringstream:
C++
// C++ program to convert // int to char using // stringstream #include <iostream> using namespace std; #include <sstream> // Driver code int main() { int N = 1234; std::stringstream t; t << N; char const *n_char = t.str().c_str(); printf ( "n_char: %s \n" , n_char);; return 0; } |
n_char: 1234
Method: Converting int value to char by adding 0
C++
// C++ program to convert // int to char using typecasting by adding zero #include <iostream> using namespace std; //Driver code int main() { int number = 65; char charvalue = ( char (number)+0); cout << charvalue; return 0; } |
A
Time complexity: O(1).
Auxiliary space: O(1).
Please Login to comment...