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?

Related Posts

  • 24 September

    Escape Percent Sign in printf Method in C++

    In this post, we will see how to escape percent sign in printf Method in C++. Escape percent sign in Printf Method in C++ printf() method uses percent sign(%) as prefix of format specifier. For example: To use number in prinf() method, we use %d, but what if you actually want to use percent sign […]

  • 15 September

    Remove Last Element from Vector in C++

    Table of ContentsWays to Remove Last Element from Vector in C++Using the vector::pop_back() function to remove last element from vector in C++Using the vector::resize() Function to Remove Last Element from Vector in C++Using the vector::rrase() Function to Remove Last Element from Vector in C++Conclusion Learn about how to remove last element from Vector in C++. […]

  • 19 May

    Get Type of Object in C++

    Table of ContentsWhy Should We Determine the Type of Object in C++: Dynamic DispatchingGet Type of Object in C++Conclusion C++ is an object-oriented language where we often interact with objects of different types. The object interaction becomes more interesting when we add runtime polymorphism to it. The runtime polymorphism, also known as, dynamic dispatch of […]

  • 19 May

    Convert Vector to Array in C++

    Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++Conclusion The files are used to store the data permanently on a disk. This mitigates the problem of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.