Converting Int to String in C++

It is common to convert an integer (int) to a string (std::string) in C++ programs. Because of the long history of C++ which has several versions with extended libraries and supports almost all C standard library functions, there are many ways to convert an int to string in C++. This post introduces how to convert an integer to a string in C++ using C and C++ ways and libraries. If you would like to convert a string to an integer, please check How to Convert String to Int in C++.

We can make use of the C standard library or C++ standard library functions/classes to convert an int to a string in C++.

Int to string: the “modern” C++-style way

We can use the C++ standard library std::to_string() available in standard library since C++11. This way is recommended if C++11 and later standard is used for the C++ project.

std::string to_string( int value );

Defined in header <string>.

Converts a numeric value to std::string.
1) Converts a signed decimal integer to a string with the same content as what std::sprintf(buf, “%d”, value) would produce for sufficiently large buf.

An example C++ program to use std::to_string() to convert int to string is as follows.

#include <iostream>
#include <string>

int main ()
{
  int n = 123;
  std::string str = std::to_string(n);
  std::cout << n << " ==> " << str << std::endl;

  return 0;
}

Note that std::to_string() may throw std::bad_alloc from the std::string constructor. The caller may catch the exception if the caller intends to handle it.

Int to string: the stream based C++-style way

We can use C++ standard library std::stringstream which has been available in the C++ standard library for C++ before C++11 to convert int to string. One C++ program using std::stringstream is as follows.

#include <sstream>
#include <iostream>

int main()
{
  int i = 123;
  std::stringstream ss;
  ss << i;
  std::string out_string = ss.str();
  std::cout << out_string << "\n";
  return 0;
}

Int to string: the C-style way

We can also use the C standard library functions in C++ too. One C standard library function that can do the int to string converting is snprintf().

#include <stdio.h>

int snprintf(char *str, size_t size, const char *format, ...);

The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.

Here is one C++ program that uses the snprintf() function to convert int to string.

#include <cstdio>
#include <iostream>

#define MAX_BUFFER_SIZE 128

int main()
{
  int number = 123;
  char out_string [MAX_BUFFER_SIZE];
  int rt = snprintf(out_string, MAX_BUFFER_SIZE, "%d", number);
  if (rt < 0) {
    std::cerr << "snprintf() failed with return code " << rt << std::endl;
  } else {
    std::cout << "out_string = \"" << out_string << "\"\n";
  }
  return 0;
}

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

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