There are different ways to convert int to char array in C++ that we will explain in this article.
[toc]
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:
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:
#includeusing 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< Output:
Number converted to char array is: 9876Using
stringstreamto convertinttoChar Arrayin C++In this method we will use the
stringstreamclass. 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:
#include#include using namespace std; int main() { int n=9876; //number to be converted stringstream temp_str; temp_str< Output:
Number converted to char array is: 9876Using
to_charsmethod to convertinttoChar Arrayin C++In this method, we will utilize the
to_charsmethod 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:
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 into_chars_resultCode:
#include#include 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< Output:
Number converted to char array is: 9876Using
sprintffunction to convertinttoChar Arrayin C++
sprintfstands 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():
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:
#includeusing 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< Output:
Number converted to char array is: 9876Using
Boost:: lexical_castmethod to convertinttoChar Arrayin 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.hppin the header that provides a casting operator(boost: lexical_cast).Code:
#include "boost/lexical_cast.hpp" #includeusing 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 number_array = lexical_cast >(n); cout<<"Number converted to char array is:"; cout< Output:
Number converted to char array is: 9876Conclusion
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!!