C++

C++ Convert Int to String

The conversion of data into two data types is a common phenomenon in the C++ programming language. Similarly, the conversion of integer-type data into the string is described in this guide. Both data types can be converted in several ways.

The integer data type is a simple type of variable that stores the data only in the form of variables. There are no specified functions that are applied only to the integers. Whereas the string is a data type that has several functions by default that are applied to them. Strings store data in the form of inverted commas. We have converted integer and float values in the string by using one of its built-in functions. For strings, it is mandatory to use a string library.

Implementation to Convert Integer to String

Example 1

The process of converting an integer into a string can be done through different approaches. But one of the basic approaches is to use a built-in function ‘to-string()’. This is the direct conversion of the input value in a string. We have taken two inputs here of different data types. One is in integer, while the other is a float value. To accomplish the conversion, we need two libraries to be added to the source code. One is ‘iostream’, which is required for the cin and cout of data in the program, as we need to display the resultant value, so we need this library. Similarly, the conversion is in the string data type, so we will use the library of string. This library is responsible for all the operations and the features applied on and to the strings in C++.

#include <iostream>

#include <string>

This program contains an involvement of a simple function of strings. The two variables are declared here. One will contain an integer value and the second will have the float value. We will apply the ‘to_string()’ function on both variables. This function contains the variable as a parameter and then converts them into a string.

String stri = to_string( i_val);

A string-type object or a variable is taken here that will store the converted string in it, and this object will be used for displaying the resultant value. A similar statement will be for the float value, as we have used for the integer.

After writing the source code in the file, we will save it and then compile the code by using a compiler g++. -O keyword will be used to save the output in an output file, and the input file in which you have written the source code, will be used.

$ g++ -o int int.c

$ ./int

On execution, you will see that both the integer and the float values have become strings. The float value contains extra digits in the resultant value, this is because of the to_string() function. Now, a question arises how will you confirm that the conversion is done successfully, or these values are strings or still integers? Because there is no hint or any point that confirms that it’s a string.

This ambiguity is valid, as the string function used above is a complete method, but still, the resultant values are not sufficient to explain.

So, we will apply a built-in function of strings just to check that both resultant values are in a string. This function appends(). this feature is capable of adding two strings. The requirement for this is that both the values that are to be added must be strings, otherwise, this function will not work. That’s why we will apply this function when both the input values are converted into strings. So, we have applied this function on both the values in strings after the conversion from the integer and float values.

Stri.append(strf);

As stri contains the string of integers, and strf contains the string of floats.

After applying this function, we will display the results of this function.

You will see that both the strings are added through this append function. Hence, it is proved that both the converted values are strings.

Example 2

This example deals with the involvement of string streams. These are the parts of strings but not included in the basic string library, so we need to add a separate header file for this purpose. This is a ‘sstream’ library that is a string stream. Include a separate library for the string as well, as we need to apply any basic string operation in the source code.

#include <iostream>

#include <sstream>

#include <string>

A stringstream class is a stream class that is included to be applied only on strings. It implements the input and output operations on the memory base streams. A stringstream is associated with a string object that allows us to read from any string. Some basic methods that are included in streams are clear(), operator <<, str(). In this program, we will use operator <<, which is used to add a string to the stringstream object. And also str() function to set the object of string whose contents are present in the stream.

Inside the main program, we will take an integer value, which we will transform into a string. After that, an output string stream will be declared by declaring the object of the string.

Ostringstream str1.

After this declaration, we will send the number that is present in the variable as a stream to the output string.

Str1 << num;

String gek = str1.str ()

After that, the contents will be displayed. Now, the resultant value is the same number but the data type of the integer value is changed into a string.

Through a compiler, when you execute the program, the number will be displayed.

Example 3

The third approach that is useful in the conversion of integer to string is the use of a lexical_cast(). A lexical cast is used and first defined in the library “boost/lexical_cast.hpp”, this provides a casting operator that is used to convert an integer to a string or vice versa.

#include <boost/lexical_cast.hpp>

The rest of the libraries are the same as described in the above examples for the string and the input/output stream.

Inside the main program, a float and an integer values are declared. Both values will be converted into a string. The function of the lexical cast will take the variable in the parameter for the conversion, and then the converted value will be stored in a string-type variable.

String strf = boost :: lexical_cast<string>(f_val);

The same statement will be used for the float value as well. Display the strings through the string objects that have stored the returned converted values.

On execution, the result will be as follows.

As we have confirmed the resultant values as a string in the first example, you can check through that method for the other two examples as well.

Conclusion

The ‘int to string C++’ is an article on the conversion of data in between two basic data types of C++. Three basic methodologies are highlighted, including ‘to_string()’ function, ‘streamstring’, and the boost/lexical_cast() function. We hope each method will be useful for the users in the implementation purpose.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.