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: A
Input: 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++
#include <iostream>
using namespace std;
int main()
{
int N = 97;
cout << char (N);
return 0;
}
|
The time complexity is O(1) and an auxiliary space is O(1).
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++
#include <iostream>
using namespace std;
int main()
{
int N = 65;
char c = N;
cout << c;
return 0;
}
|
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++
#include <iostream>
using namespace std;
int main()
{
int N = 65;
char c = static_cast < char >(N);
cout << c;
return 0;
}
|
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++
#include <iostream>
using namespace std;
#define Max_Digits 10
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;
}
|
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++
#include <iostream>
using namespace std;
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;
}
|
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++
#include <iostream>
using namespace std;
#include <sstream>
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;
}
|
Method: Converting int value to char by adding 0
C++
#include <iostream>
using namespace std;
int main()
{
int number = 65;
char charvalue = ( char (number)+0);
cout << charvalue;
return 0;
}
|
Time complexity: O(1).
Auxiliary space: O(1).
Approach: ASCII value offset approach
Steps:
- Take an integer input from the user.
- Check if the input value corresponds to a valid character in the ASCII table by checking the range of the input value.
- If the input value corresponds to a valid character, then add the corresponding offset value of ‘0’ or ‘A’ (depending on the input) to the integer value to get the corresponding character value.
- Output the corresponding character.
C++
#include <iostream>
using namespace std;
int main() {
int num = 65;
cout << "Enter an integer: " << num << endl;
char ch;
if (num >= 65 && num <= 90) {
ch = num;
} else if (num >= 97 && num <= 122) {
ch = num;
} else {
cout << "Invalid input." << endl;
return 0;
}
cout << "The corresponding character is: " << ch << endl;
num = 97;
cout << "Enter an integer: " << num << endl;
if (num >= 65 && num <= 90) {
ch = num;
} else if (num >= 97 && num <= 122) {
ch = num;
} else {
cout << "Invalid input." << endl;
return 0;
}
cout << "The corresponding character is: " << ch << endl;
return 0;
}
|
OutputEnter an integer: 65
The corresponding character is: A
Enter an integer: 97
The corresponding character is: a
Time Complexity: O(1), as there are no loops involved.
Auxiliary Space: O(1), as we are only using a single character variable to store the result.
Approach Name: Arithmetic Conversion
Steps:
- Calculate the number of digits in the input int value.
- Iterate through the digits from right to left, extracting each digit and adding the ASCII value of ‘0’ to convert it to a char.
- Store the resulting char array in the provided output buffer.
C++
#include <iostream>
#include <cstring>
using namespace std;
void int_to_char( int num, char *result) {
int temp = num;
int len = 0;
while (temp > 0) {
len++;
temp /= 10;
}
for ( int i = len - 1; i >= 0; i--) {
result[i] = num % 10 + '0' ;
num /= 10;
}
result[len] = '\0' ;
}
int main() {
int num = 12345;
char result[100];
int_to_char(num, result);
cout << result << endl;
return 0;
}
|
Time complexity: O(log10 n), where n is the input int value.
Space complexity: O(log10 n), where n is the input int value, due to the need to store the output char array.