Convert int to Char Array in C++

Convert int to char array in C++

There are different ways to convert int to char array in C++ that we will explain in this article.

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:

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:

Output:

Number converted to char array is: 9876

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:

Output:

Number converted to char array is: 9876

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:

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:

Output:

Number converted to char array is: 9876

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():

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:

Output:

Number converted to char array is: 9876

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:

Output:

Number converted to char array is: 9876

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!!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *