C++ Program to Convert int to char
Overview
While working with many data types in the C++ programming language, sometimes we need to convert one kind of data type to another kind of data type. Converting an integer to a character in C++ might seem one-way or straightforward, but several techniques can be used. In this article, you will see various methods to convert int to char C++. All the processes are explained with code examples and explanations for better understanding. Before proceeding in the article, let us see how many conversion types are for into to char C++.
They are as follows:
- Using explicit Typecasting
- Using static_cast
- Using sprintf
- Using to_string and c_str
- Using to_chars
- Using stringstream
Now, let us see all these methods individually in the further section.
C++ Program to Convert int to char Using Explicit Typecasting
Explicit typecasting is defined as the process of directly converting one data type to another data type. Explicit method involves the use of standard C++ libraries, such as <iostream>, <charconv>, etc.
In this case, we will convert an int to a char using typecasting. We will use the static_cast operator to perform this conversion.
You can convert the int to char C++ in the given three steps:
-
Declaration and Initialization:
This step involves the declaration and initialising of the integer value you want to convert int to char in C++.
-
Typecasting:
It refers to the process of converting one data type into another data type. For example, if you flip the int data type to float, etc.
-
Printing the char:
It is the final step of the process. Here, you print the converted result using the cout function.
In the provided code example, we start with an integer value num representing the ASCII value of the character we want to convert. By using static_cast<char>(num), we instruct the compiler to convert the integer value to its corresponding character representation. The result will be stored in the variable “c” and will be printed.
Output:
C++ Program to Convert int to char Using static_cast
A static_cast c++ operator is a kind of unary operator. It is used for the conversion of one data type to another. This is the most basic cast available. The static_cast takes a long time to compile, and it can do implicit type conversions (such as int to float or pointer to void*).
Like the previous method, we used static_cast to convert an integer to a character. The code is nearly identical to the previous one, with the main difference being the value of num, which represents a different ASCII character. This approach showcases the use of typecasting in a controlled and defined manner.
Output:
C++ Program to Convert int to char Using sprintf()
The sprintf() function allows converting data into strings. We can leverage it to convert an int to a char array. The sprintf()function is found in the Standard C Library, and it is used for formatting text. In this context, we use it to convert an integer to a character array. The %c format specifier instructs sprintf() to treat the provided integer as a character and store it in the character array.
There are three parameters used in this method, which are as follows:
- cstr – pointer to the char array where the resulting c-string will be stored.
- format – format specifier of the value.
- value – the integer to convert.
Output:
C++ Program to Convert int to char Using to_string() and c_str()
The to_string() function converts numerical values to string format, and c_str() converts a C++ string to a C-style string (char array). The to_string() function converts the integer num into a string. Then, using c_str(), we obtain a C-style string (char array) from the C++ string. This array contains the character representation of the integer.
Output:
C++ Program to Convert int to char Using to_chars()
The to_chars() method directly converts the int to a char value. This method is defined in the <charconv> header file and converts an integer or a floating-point number into a sequence of char. This method provides better performance. The to_chars() function offers a more efficient way to convert values to characters. In this method, we have used the to_chars() function to convert an integer into a character directly.
The three parameters that are used to pass to the to_chars(first, last, value) are as follows:
- first – Pointer is the beginning of the char*
- last – Pointer to the last of the char*.
- value – The integer that needs to be converted.
Output:
C++ Program to Convert int to char Using stringstream
Stringstream allows the user to perform input and output operations on strings. It can be used for this conversion. The stringstream class allows you to perform input and output operations on strings. In this approach, you can insert the integer value into the stringstream object using the insertion operator (<<). Then, you can extract the character value from the stringstream using the str() method to obtain the string representation. Finally, you can access the string's first character to get the desired character representation as your program output.
Output:
Conclusion
- There are various methods to convert int to char C++.
- These methods are by using typecasting, using static_cast, using sprintf() using to_string() and c_str(), using to_chars(), using stringstream.
- Explicit typecasting is defined as the process of directly converting one data type to another data type.
- You can use the static_cast<char>(num) to instruct the compiler to convert the integer value to its corresponding character representation.
- The sprintf() function is also used to convert the input data into string values.
- You can use the %c format specifier, which instructs sprintf() to treat the provided integer as a character and store it in the character array element.
- The to_chars() function directly converts values to characters and provides better performance.
- Stringstream allows the user to perform input and output operations on strings.