Register Login

Convert an Number to a String in C++

Updated May 11, 2023

Data type conversion is one of the standard practices among programmers, which can be efficient and fast. There are several applications for the int to string conversion in C++, and this article will discuss all the methods to convert a C++ int into a string.

Implicit and explicit conversions are the two types of conversions that users can apply for the C++ conversion of two data types. Users can use built-in methods to perform explicit type conversion, like other programming languages.

What are int and string in C++?

The keyword int stores only integer type data and can store data within -2147483648 to 2147483647, usually of size 4 bytes. Int, short for integer, is one of the primary variable types built into the compiler.

On the other hand, users use string data types to store values in characters, letters, and symbols. These contain a collection of characters within single or double quotations.

When and Why Do We Need to Convert Int to String in C++?

Conversion of any integer-type data format to a string allows users to accomplish different string operations on that data. Often, arithmetic operations become a soulless task compared to string operations.

Thus sometimes, users need to convert the int into string data type to perform specific tasks. When users want to save their int-type data into text format data in a file, they need to convert the int to string in C++, and when they try to display an int to the console for visual purposes.

Here are some methods that can help convert an int data type into a string:

Method 1: Using stringstream Class in C++

The stringstream class in C++ is a derived class of iostream that allows executing several operations on streams based on strings like insertion, extraction, etc. Users can perform parsing in different ways and include the “sstream” header file. This C++ class contains the following methods:

  • Operator ">>": Users can use the ">>" operator in the stringstream class to read or fetch formatted data from a stream object.
  • Operator "<<": Users can use the "<<" operator in the stringstream class to add formatted data into a stream object.
  • str(): Users can use the str() method of the stringstream class to assign or get the elements of the underlying string object.
  • clear(): Users can use the clear() method of the stringstream class to clear the stream contents.

Code Snippet:

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
	int n = 35;
	stringstream stream;
	stream << n;
	string s;
	stream >> s;
	cout << "\n"
	<< "The value if the number is: " << n << "\n";
	cout << "Users can represent this number in the string after applying stringstream is: " << s;
	return 0; 
}

Output:

Explanation:

In the above example, we used the stringstream class to convert the input int into a string. Here, we initialized a variable "n" with integer data. Then we used the stringstream stream that converts the integer 35 into the string "35."

This stream object executes operations like insertion and extraction utilizing the "<<" and ">>" operators. We first inserted the integer 35 into the object stream and then extract the number to a string variable "n."

Method 2: Using the to_string() method

Another method of the class string or <string> or <cstring> allows users to convert an int data type into a string after taking numerical value as its parameter. It can take any value, like an integer, float, double, long double, etc., to convert it into a string.

Following are the syntaxes of the to_string() method:

  • string to_string (int num);
  • string to_string (long long num);
  • string to_string (float num);
  • string to_string (long num);
  • string to_string (unsigned num);
  • string to_string (long double num)
  • string to_string (unsigned long num);
  • string to_string (double num);
  • string to_string (unsigned long long num);

Code Snippet:

#include <iostream>
#include <string> 
using namespace std; 
int main()
{
    int a = 10;
    int b = 1010;
    int c = 1010100;
    string x = to_string(a);
    string y = to_string(b);
    string z = to_string(c);
    cout << "We are representing the number declared in the variable a into string: " << x << '\n';
    cout << "We are representing the number declared in the variable b into string: " << y << '\n';
    cout << "We are representing the number declared in the variable c into string: " << z << '\n';
    return 0;
}

Output:

Explanation:

In this code snippet, we used the to_string that explicitly converts the initialized integer value into a string. We declared three integers under three variables.

Then we passed each integer value to the to_string() method to retrieve their string representation. But before running the program, make sure you added the <string> header file to use this functionality of the string class.

Method 3: Using the boost::lexical_cast

The last method involved the boost::lexical_cast, where users can convert data from strings to numeric types such as int or double and vice versa. Thus, users can easily convert an integer data type into a string and come under the library "boost/lexical_cast.hpp."

Code Snippet:

#include <bits/stdc++.h>
#include <boost/lexical_cast.hpp>
using namespace std;
int main()
{
    int a = 765;
    string s = boost::lexical_cast<string> (a);
    cout << " Here, we represent the number a into a string: " << s << "\n";
    return 0;
} 

Output:

Explanation:

Here, we used the boost::lexical_cast that accepts the integer 765 as the lexical_cast operator and the integer into the corresponding string "765."

Conclusion:

We hope the article has given a crisp idea of converting an integer data type into a string using the different approaches in C++. We highlighted why and when users need the data type conversion in C++, including the conversion techniques, their syntaxes, and code snippets.


×