There are different ways to convert int to char array in C++ that we will explain in this article.
Table of Contents
- Using to_string and c_str methods to convert int to Char Array in C++
- Using stringstream to convert int to Char Array in C++
- Using to_chars method to convert int to Char Array in C++
- Using sprintf function to convert int to Char Array in C++
- Using Boost:: lexical_cast method to convert int to Char Array in C++
- Conclusion
Using to_string
and c_str
methods to convert int
to Char Array
in C++
In this method, we will first convert the number to a string by utilizing the to_string
method in C++. Then we use the c_str method which will return a pointer to a character array that will contain a sequence of characters that will be terminated by null character. These sequences of characters represent the current value of the string object.
Syntax:
1 2 3 |
to_string: string str=to_string(int val); |
It can be used to convert int, long, float, double, long long, unsigned, unsigned long, long double)
c_str: const CharA* c_str() const
check the implementation in the example given below.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; int main() { int n=9876; //number to be converted string temp_str=to_string(n); //converting number to a string char const* number_array= temp_str.c_str(); //converting string to char Array cout<<"Number converted to char array is: "; cout<<number_array[0]; cout<<number_array[1]; cout<<number_array[2]; cout<<number_array[3]; return 0; } |
Output:
Using stringstream
to convert int
to Char Array
in C++
In this method we will use the stringstream
class. We will create a temporary string stream where we will store the int data, then we will return the string object with the help of str() method. In the end, we will use the c_str() method to complete the conversion.
str(): This method is used to return the string object along with the copy of the current elements of the string stream.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <sstream> using namespace std; int main() { int n=9876; //number to be converted stringstream temp_str; temp_str<<n; //passing number to the stream char const *number_array = temp_str.str().c_str();//converting to char array cout<<"Number converted to char array is: "; cout<<number_array[0]; cout<<number_array[1]; cout<<number_array[2]; cout<<number_array[3]; return 0; } |
Output:
Using to_chars
method to convert int
to Char Array
in C++
In this method, we will utilize the to_chars
method in C++ and it was added in C++17 in the header . This method can be used to convert an integer or float value to a sequence of characters. It can convert a value into a character string by filling them in a range [first, last). (Here range [first, last) should be valid.)
Syntax of to_chars:
1 2 3 |
to_chars_result to_chars(char* first, char* last, int value, int base = 10); |
first
: It points to the start position of the buffer to fill.
last
: It points to the last position of the buffer to fill.
Value
: It stores a value which we need to convert. (If the value passed is negative, then it should start with a -
sign)
base
: For converting integers, we can use base between 2 and 36 when we convert value to chars. There should be no leading zeros.
Result will be stored in to_chars_result
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <charconv> using namespace std; #define MAX_DIGITS 15 int main() { int n=9876; //number to be converted char number_array[MAX_DIGITS + sizeof(char)]; //conversion to char array to_chars(number_array, number_array + MAX_DIGITS, n); cout<<"Number converted to char array is:"; cout<<number_array[0]; cout<<number_array[1]; cout<<number_array[2]; cout<<number_array[3]; return 0; } |
Output:
Using sprintf
function to convert int
to Char Array
in C++
sprintf
stands for- String Print. This function does not print on console, but it stores the output on the char buffer which is mentioned in the sprintf. It will return all the characters except the null character that is appended in the string. In the case of failure, it will return a negative number.
Syntax of sprintf():
1 2 3 |
int sprintf ( char * string, const char* format_specifier, …); |
string: It points to the string buffer that will store the resulting string.
format_specifier: It points to a null terminated string. It consists of characters and optional format specifiers that start with %
. The format specifiers that follow the format string are also replaced by their respective variables which contain values.
Note: The size of the buffer used in this method should be large enough to store the complete resulting string.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; #define MAX_DIGITS 15 int main() { int n=9876; //number to be converted char number_array[MAX_DIGITS + sizeof(char)]; //conversion to char array //"%d" format specifier is used for integers sprintf(number_array, "%d", n); cout<<"Number converted to char array is:"; cout<<number_array[0]; cout<<number_array[1]; cout<<number_array[2]; cout<<number_array[3]; return 0; } |
Output:
Using Boost:: lexical_cast
method to convert int
to Char Array
in C++
In this method, we will utilize the powerful Boost library of C++ to convert int to char array. We can use boost/lexical_cast.hpp
in the header that provides a casting operator(boost: lexical_cast).
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include "boost/lexical_cast.hpp" #include <bits/stdc++.h> using namespace std; using boost::lexical_cast; using boost::bad_lexical_cast; int main() { int n=9876; //number to be converted //conversion to char array array<char, 64> number_array = lexical_cast<array<char, 64>>(n); cout<<"Number converted to char array is:"; cout<<number_array[0]; cout<<number_array[1]; cout<<number_array[2]; cout<<number_array[3]; return 0; } |
Output:
Conclusion
In this article, we explained interesting and efficient ways to convert int to char array in C++. Any method can be used as per the requirements.
Happy Learning!!