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

Related Posts

  • 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 […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusion An array is a […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusion This article discusses the vector and how to print vector in C++. Vectors are […]

  • Print Map in C++
    15 March

    Print Map in C++

    Table of ContentsMap vs Unordered MapPrint Map in C++ Using IteratorPrint Map in C++ Using Range-Based for LoopPrint Map in C++ by Overloading the ostream Insertion Operator (<<)Print Map in C++17 Using Structure Binding and Range-Based for LoopPrint Map in C++17 Using for_each() FunctionConclusionReferences Maps in C++ store the data in form of key and […]

  • How to write binary files in C++
    14 March

    Write Binary Files in C++

    Table of ContentsOpening a File in Binary Mode Using fstream LibraryWriting the Binary File Using the write() Function of fstream LibraryWriting the Binary File Using the put() Function of fstream LibraryWriting the Binary File Using Insertion Operator (<<) Of fstream LibraryWriting the Structure Object to Binary File Using fstream LibraryConclusion In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.