2061

What is the easiest way to convert from int to equivalent string in C++? I am aware of two methods. Is there an easier way?

(1)

int a = 10;
char *intStr = itoa(a);
string str = string(intStr);

(2)

int a = 10;
stringstream ss;
ss << a;
string str = ss.str();
7
  • 6
    I think both methods you gave are good solutions. it depends on the context where you need to do it. If you're already working with streams, for example reading or writing a file, then your second method is the best. If you need to pass an int as a string to a function argument, then itoa could be an easy way. But most of the time, int to string conversion occurs when dealing with files, so streams are appropriate. Apr 8, 2011 at 5:21
  • 62
    How does option 1 even work for you at all? It's my understanding that itoa() takes three parameters.
    – arkon
    Apr 10, 2013 at 2:49
  • 2
    itoa will be faster than the stream equivalent. There are also ways of re-using the string buffer with the itoa method (avoiding heap allocations if you are frequently generating strings. e.g. for some rapidly updating numerical output). Alternatively you can generate a custom streambuf to reduce some of the allocation overhead etc. Constructing the stream in the first place is also not a low cost venture.
    – Pete
    Aug 28, 2013 at 18:46
  • 7
    @Pete: Once you start worrying about which is faster, you'll want to look at stackoverflow.com/questions/4351371/…
    – Ben Voigt
    Sep 24, 2013 at 19:21
  • 25
    Note that itoa() is not part of the standard and therefore using it renders your code not portable since not all compilers support it. For Linux you are most certainly out unless you are using something else than GCC, which does not support this function. If you have C++0x, go with what @Matthieu has suggested in his answer. If that's not the case, go with stringstream since it is a well supported feature and your code should be compatible with every C++ compiler out there. As an alternative you can always go with sprintf(). Jun 16, 2014 at 9:59

31 Answers 31

2711

C++11 introduces std::stoi (and variants for each numeric type) and std::to_string, the counterparts of the C atoi and itoa but expressed in term of std::string.

#include <string> 

std::string s = std::to_string(42);

is therefore the shortest way I can think of. You can even omit naming the type, using the auto keyword:

auto s = std::to_string(42);

Note: see [string.conversions] (21.5 in n3242)

22
  • 218
    to_string not a member of std fix: stackoverflow.com/questions/12975341/…
    – Ben
    Nov 2, 2012 at 3:02
  • 42
    Or depending on your compiler, just set the right language standard: g++ -std=c++11 someFile.cc Nov 29, 2012 at 23:12
  • 15
    @Steve: it's supposed to be. It's a member of std in every compiler I know of except for one. May 31, 2013 at 21:34
  • 6
    @Matthiew M. I am using the same which you suggest but i am getting this error : Error : No instance of overloaded function "std::to_string" matches the argument list i am using VS2010 c++
    – user2643530
    Sep 26, 2013 at 13:51
  • 23
    @Flying: under VS2010 you have to explicitly cast the converting integer to one of the following types [_Longlong, _ULonglong, long double]; i.e: string s = to_string((_ULonglong)i);
    – Zac
    Dec 6, 2013 at 14:50
229

C++20: std::format would be the idiomatic way now.


C++17:

Picking up a discussion with @v.oddou a couple of years later, C++17 has delivered a way to do the originally macro-based type-agnostic solution (preserved below) without going through macro ugliness.

// variadic template
template < typename... Args >
std::string sstr( Args &&... args )
{
    std::ostringstream sstr;
    // fold expression
    ( sstr << std::dec << ... << args );
    return sstr.str();
}

Usage:

int i = 42;
std::string s = sstr( "i is: ", i );
puts( sstr( i ).c_str() );

Foo x( 42 );
throw std::runtime_error( sstr( "Foo is '", x, "', i is ", i ) );

C++98:

Since "converting ... to string" is a recurring problem, I always define the SSTR() macro in a central header of my C++ sources:

#include <sstream>

#define SSTR( x ) static_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()

Usage is as easy as could be:

int i = 42;
std::string s = SSTR( "i is: " << i );
puts( SSTR( i ).c_str() );

Foo x( 42 );
throw std::runtime_error( SSTR( "Foo is '" << x << "', i is " << i ) );

The above is C++98 compatible (if you cannot use C++11 std::to_string), and does not need any third-party includes (if you cannot use Boost lexical_cast<>); both these other solutions have a better performance though.

10
  • 2
    I am not very familiar with dynamic_cast but I am using clang to compile so it complains about it. If I just omit the dynamic_cast then it compiles fine; what purpose does the dynamic_cast serve in this case? We are already creating an ostringstream, so why cast it?
    – Mathew
    Oct 21, 2014 at 2:38
  • 2
    @Mathew: The link in my answer leads to a detailed description of each part of the construct. While we created a ostringstream, we called operator<<() on it, which returns ostream & -- for which .str() is not defined. I really wonder how clang would make this work without the cast (or why it generates an error with it). This construct is published in many places, and I've used it for over a decade on many different compilers, including MSVC, GCC, and XLC, so I am rather surprised clang balks at it.
    – DevSolar
    Oct 21, 2014 at 6:59
  • 9
    Just came to the party for curiosity, and downvoted. Reason : too much votes for a solution that's un-elegant, and likely slow. 1. macro usage. I don't systematically frown on any macro, but this one is too short, and end-clients always fear repetition of the argument, on top of fear for unprotected multilines macros. (not protected by do{}while(0)) 2. dynamic_cast. it seems you only need a static_cast here, unless you want to assert that the library indeed is implemented as you hope. in which case you should use boost::polymorphic_downcast instead.
    – v.oddou
    Jun 18, 2015 at 3:30
  • 2
    @v.oddou: You're free to critizise, of course. But 1. is invalid -- the macro is a single statement, do { } while( 0 ) would not add anything. With 2. and 3. you probably got a point -- this could be done with a static cast, and perhaps one of you template wizards out there could come up with a "nicer" interface. But as I said, this is by no means an invention of myself. Look around, this macro (macro!) is quite ubiquitous. That's a case of POLA in itself. I might toy with this a bit to make it more "streamlined".
    – DevSolar
    Jun 18, 2015 at 8:45
  • 1
    @v.oddou: Look at what I found among the things C++17 brought us. :-) I hope you like the updated answer.
    – DevSolar
    May 13, 2018 at 20:57
138

Current C++

Starting with C++11, there's a std::to_string function overloaded for integer types, so you can use code like:

int a = 20;
std::string s = std::to_string(a);
// or: auto s = std::to_string(a);

The standard defines these as being equivalent to doing the conversion with sprintf (using the conversion specifier that matches the supplied type of object, such as %d for int), into a buffer of sufficient size, then creating an std::string of the contents of that buffer.

Old C++

For older (pre-C++11) compilers, probably the most common easy way wraps essentially your second choice into a template that's usually named lexical_cast, such as the one in Boost, so your code looks like this:

int a = 10;
string s = lexical_cast<string>(a);

One nicety of this is that it supports other casts as well (e.g., in the opposite direction works just as well).

Also note that although Boost lexical_cast started out as just writing to a stringstream, then extracting back out of the stream, it now has a couple of additions. First of all, specializations for quite a few types have been added, so for many common types, it's substantially faster than using a stringstream. Second, it now checks the result, so (for example) if you convert from a string to an int, it can throw an exception if the string contains something that couldn't be converted to an int (e.g., 1234 would succeed, but 123abc would throw).

2
  • 2
    Nice, I prefer Kevin's answer, though as he shows the include and namespace. Just a minor gripe. :) Good job, though! Mar 21, 2013 at 14:15
  • 5
    I'd say this is the way to go if you don't have C++11 support. Jun 21, 2013 at 8:16
122

I usually use the following method:

#include <sstream>

template <typename T>
  std::string NumberToString ( T Number )
  {
     std::ostringstream ss;
     ss << Number;
     return ss.str();
  }

It is described in details here.

7
  • 18
    @lifebalance: I have never seen such behavior.
    – Rasoul
    Jun 11, 2015 at 8:23
  • 26
    @lifebalance: You do not need to clear() a newly created ostringstream object. clear() resets the error/eof flags, and there has not been any error/eof condition generated yet. Aug 5, 2015 at 4:00
  • 2
    @Rasoul NumberToString(23213.123) produces 23213.1 while std::to_string(23213.123) produces 23213.123000 What happens there? Nov 5, 2017 at 23:00
  • 1
    I wouldn't normally write a function for three lines, but it does make it self documenting via the name. Clarity vs self documenting -- those shouldn't conflict, right?
    – phorgan1
    Nov 23, 2018 at 2:04
  • 1
    @KillzoneKid This is because std' ostream are stateful (this means that any previous state change is kept, like the number of decimal digit) while this method starts with a default state.
    – xryl669
    Jun 14, 2019 at 14:04
54

You can use std::to_string available in C++11 as suggested by Matthieu M.:

std::string s = std::to_string(42);

Or, if performance is critical (for example, if you do lots of conversions), you can use fmt::format_int from the {fmt} library to convert an integer to std::string:

std::string s = fmt::format_int(42).str();

Or a C string:

fmt::format_int f(42);
const char* s = f.c_str();

The latter doesn't do any dynamic memory allocations and is more than 70% faster than libstdc++ implementation of std::to_string on Boost Karma benchmarks. See Converting a hundred million integers to strings per second for more details.

Disclaimer: I'm the author of the {fmt} library.

4
  • 2
    I was curious about the claim of not having any dynamic memory allocation while remain threadsafe (re-entrant), so I read your code -- the c_str() returns a pointer to a buffer declared inside the fmt::FormatInt class -- so the pointer returned will be invalid at the semicolon -- see also stackoverflow.com/questions/4214153/lifetime-of-temporaries
    – Soren
    Jun 16, 2014 at 22:17
  • 1
    Yes, the same behavior as with std::string::c_str() (thus the naming). If you want to use it outside of the full expression construct an object FormatInt f(42); Then you can use f.c_str() without a danger of it being destroyed.
    – vitaut
    Jun 16, 2014 at 23:40
  • 1
    I get something weird when I try to convert from int to string using std::to_string(num). If I store the result in a variable and try to Access it like stringNum[1] or stringNum[n] as n increases, I get garbage. Feb 9, 2018 at 7:25
  • 1
    That's definitively the right answer to this question: efficient and standard code that's not stateful.
    – xryl669
    Dec 21, 2019 at 22:47
45

If you have Boost installed (which you should):

#include <boost/lexical_cast.hpp>

int num = 4;
std::string str = boost::lexical_cast<std::string>(num);
1
  • 5
    Agreed on boost installation. I think that more than often one would format the string. For this purpose I prefer boost::format e.g format("%02d", number ).str() Aug 28, 2013 at 18:47
39

It would be easier using stringstreams:

#include <sstream>

int x = 42;          // The integer
string str;          // The string
ostringstream temp;  // 'temp' as in temporary
temp << x;
str = temp.str();    // str is 'temp' as string

Or make a function:

#include <sstream>

string IntToString(int a)
{
    ostringstream temp;
    temp << a;
    return temp.str();
}
0
20

Not that I know of, in pure C++. But a little modification of what you mentioned

string s = string(itoa(a));

should work, and it's pretty short.

5
  • 69
    itoa() is not a standard function!
    – cartoonist
    Nov 15, 2012 at 11:25
  • 4
    @cartoonist: Then what is it?
    – user541686
    Nov 15, 2012 at 15:35
  • 25
    This function is not defined in ANSI-C and C++. So it's not supported by some compiler such as g++.
    – cartoonist
    Nov 15, 2012 at 20:49
  • 1
    cplusplus.com/reference/cstdlib/itoa: "Portability This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers." gcc.godbolt.org/z/MaEEf4evW
    – kfsone
    Oct 7, 2021 at 17:55
  • @cartoonist I think it has to do with glibc, not g++. I wish they just added all these functions to glibc. Would make things easier for people Mar 22 at 15:39
18

sprintf() is pretty good for format conversion. You can then assign the resulting C string to the C++ string as you did in 1.

9
  • 2
    Heh, yes. However, I usually rely on snprintf() and friends for anything of consequence when handling C strings. Apr 8, 2011 at 12:42
  • 1
    @MatthieuM. Your comment proves further, that you are not. If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. So you call it with a NULL and zero size to get the necessary buffer size. Sep 25, 2013 at 8:02
  • 2
    @user1095108: I think you are mistaking snprintf (note the SNP prefix) and sprintf (note the SP prefix). You pass the size to the former, and it takes care not to overflow, however the latter knows not the size of the buffer and thus may overflow. Sep 25, 2013 at 9:37
  • 1
    The idea is to call a snprintf variant first and a sprintf variant after that. As the buffer size is known by then, calling sprintf becomes entirely safe. Sep 25, 2013 at 9:40
  • 1
    @user1095108 Ah, yes. If the first buffer to snprintf is insufficient, the return value tells you what would be sufficient. I would still use snprintf for the second call, because relying on the implementations of sprintf and snprintf to match is unnecessarily dangerous.
    – mabraham
    Oct 5, 2013 at 8:02
15

Using stringstream for number conversion is dangerous!

See std::ostream::operator<< where it tells that operator<< inserts formatted output.

Depending on your current locale an integer greater than three digits, could convert to a string of four digits, adding an extra thousands separator.

E.g., int = 1000 could be converted to a string 1.001. This could make comparison operations not work at all.

So I would strongly recommend using the std::to_string way. It is easier and does what you expect.

From std::to_string:

C++17 provides std::to_chars as a higher-performance locale-independent alternative.

4
  • 2
    I agree that this is a serious problem if you need to exchange data. Unfortunately, also std::to_string uses the current locale (see en.cppreference.com/w/cpp/string/basic_string/to_string , the 'Notes' section). Almost all standard tools (from stringstreams to sprintf, but also sscanf etc) are using the current locale. I wasn't aware of this until recently when it hit me hard. Currently using home-grown stuff, not hard to make.
    – Bert Bril
    Oct 24, 2017 at 22:05
  • In the link above it is also statet that C++17 provides std::to_chars as a higher-performance locale-independent alternative. Jan 10, 2018 at 12:01
  • Unfortunately, I am stuck with C++11 for the coming year(s) (quite an improvement already, luckily).
    – Bert Bril
    Jan 12, 2018 at 21:39
  • 1
    from_chars and to_chars would be perfect but unfortunately they didn't offer a wchar_t variant.
    – gast128
    Oct 10, 2019 at 10:37
14

First include:

#include <string>
#include <sstream>

Second add the method:

template <typename T>
string NumberToString(T pNumber)
{
 ostringstream oOStrStream;
 oOStrStream << pNumber;
 return oOStrStream.str();
}

Use the method like this:

NumberToString(69);

or

int x = 69;
string vStr = NumberToString(x) + " Hello word!."
0
11

In C++11 we can use the "to_string()" function to convert an int into a string:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int x = 1612;
    string s = to_string(x);
    cout << s<< endl;

    return 0;
}
9

C++17 provides std::to_chars as a higher-performance locale-independent alternative.

1
  • 1
    For performance intensive applications you need this one and not the stringstream route.
    – gast128
    Sep 11, 2020 at 20:39
6

If you need fast conversion of an integer with a fixed number of digits to char* left-padded with '0', this is the example for little-endian architectures (all x86, x86_64 and others):

If you are converting a two-digit number:

int32_t s = 0x3030 | (n/10) | (n%10) << 8;

If you are converting a three-digit number:

int32_t s = 0x303030 | (n/100) | (n/10%10) << 8 | (n%10) << 16;

If you are converting a four-digit number:

int64_t s = 0x30303030 | (n/1000) | (n/100%10)<<8 | (n/10%10)<<16 | (n%10)<<24;

And so on up to seven-digit numbers. In this example n is a given integer. After conversion it's string representation can be accessed as (char*)&s:

std::cout << (char*)&s << std::endl;

Note: If you need it on big-endian byte order, though I did not tested it, but here is an example: for three-digit number it is int32_t s = 0x00303030 | (n/100)<< 24 | (n/10%10)<<16 | (n%10)<<8; for four-digit numbers (64 bit arch): int64_t s = 0x0000000030303030 | (n/1000)<<56 | (n/100%10)<<48 | (n/10%10)<<40 | (n%10)<<32; I think it should work.

0
4

It's rather easy to add some syntactical sugar that allows one to compose strings on the fly in a stream-like way

#include <string>
#include <sstream>

struct strmake {
    std::stringstream s;
    template <typename T> strmake& operator << (const T& x) {
        s << x; return *this;
    }   
    operator std::string() {return s.str();}
};

Now you may append whatever you want (provided that an operator << (std::ostream& ..) is defined for it) to strmake() and use it in place of an std::string.

Example:

#include <iostream>

int main() {
    std::string x =
      strmake() << "Current time is " << 5+5 << ":" << 5*5 << " GST";
    std::cout << x << std::endl;
}
2

Use:

#define convertToString(x) #x

int main()
{
    convertToString(42); // Returns const char* equivalent of 42
}
3
  • 3
    Works only with literal numbers, doesn't evaluate variable content, though useful sometimes.
    – Wolf
    May 31, 2016 at 12:48
  • Right. But definitely handy at time Oct 3, 2016 at 4:26
  • 1
    Only works at compile time with literal constants numbers, i think the OP asks for a dynamic conversion, using variable integers Nov 3, 2017 at 1:48
2
int i = 255;
std::string s = std::to_string(i);

In C++, to_string() will create a string object of the integer value by representing the value as a sequence of characters.

1
  • 2
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation.
    – ysf
    Jun 17, 2020 at 18:46
1

I use:

int myint = 0;
long double myLD = 0.0;

string myint_str = static_cast<ostringstream*>(&(ostringstream() << myint))->str();
string myLD_str = static_cast<ostringstream*>(&(ostringstream() << myLD))->str();

It works on my Windows and Linux g++ compilers.

1

Here's another easy way to do

char str[100];
sprintf(str, "%d", 101);
string s = str;

sprintf is a well-known one to insert any data into a string of the required format.

You can convert a char * array to a string as shown in the third line.

0
1

If you're using MFC, you can use CString:

int a = 10;
CString strA;
strA.Format("%d", a);
2
1

C++11 introduced std::to_string() for numeric types:

int n = 123; // Input, signed/unsigned short/int/long/long long/float/double
std::string str = std::to_string(n); // Output, std::string
1
  • 4
    Hi! Could you add an explanation on why and how this provides an answer to the question? Jul 26, 2018 at 14:21
0

Use:

#include<iostream>
#include<string>

std::string intToString(int num);

int main()
{
    int integer = 4782151;

    std::string integerAsStr = intToString(integer);

    std::cout << "integer = " << integer << std::endl;
    std::cout << "integerAsStr = " << integerAsStr << std::endl;

    return 0;
}

std::string intToString(int num)
{
    std::string numAsStr;
    bool isNegative = num < 0;
    if(isNegative) num*=-1;

    do
    {
       char toInsert = (num % 10) + 48;
       numAsStr.insert(0, 1, toInsert);

       num /= 10;
    }while (num);
  
    return isNegative? numAsStr.insert(0, 1, '-') : numAsStr;
}
1
  • 4
    Completely fails for n≤0 Dec 3, 2019 at 9:17
0

All you have to do is use String when defining your variable (String intStr). Whenever you need that variable, call whateverFunction(intStr.toInt())

2
  • Can you provide a reference to toInt()? Where is "String" (uppercase "s") defined? Part of some vendor-specfic library? Are you sure it is C++? It is in the Arduino environment, but that may not count. It doesn't appear to be part of the standard string class. Oct 5, 2022 at 21:59
  • What did you test it on? Compiler, incl. version. Environment (operating system, IDE, etc.)? C++ version? Oct 30, 2022 at 14:56
0

Using the plain standard stdio header, you can cast the integer over sprintf into a buffer, like so:

#include <stdio.h>

int main()
{
    int x = 23;
    char y[2]; // The output buffer
    sprintf(y, "%d", x);
    printf("%s", y)
}

Remember to take care of your buffer size according to your needs (the string output size).

1
  • While I like your answer, it is incorrect: you forgot the null-character to mark the ending of the converted string (i.e., the size of the output string must be, at least, 3 in this case). See gnu.org/software/libc/manual/html_node/…
    – SRG
    Aug 4, 2021 at 10:21
0

use my function "inToString"

it accepts one integer argument called "number"

remember you can change the argument type to FLOAT or DOUBLE instead of INT like:

"inToString(float number)" or "inToString(double number)"

and it will work fine but make sure to give it the same type ^_^

#include<iostream>
#include<sstream>

using namespace std;


string inToString(int number){

     stringstream stream;

     stream << number;
    
     string outString;
    
     stream >> outString;

     return  outString;

 }

 int main(){

     int number = 100;

     string stringOut = inToString(number);

     cout << "stringOut : " + stringOut << endl;

     return 0;

 }
-1
string number_to_string(int x) {

    if (!x)
        return "0";

    string s, s2;
    while(x) {
        s.push_back(x%10 + '0');
        x /= 10;
    }
    reverse(s.begin(), s.end());
    return s;
}
3
  • 2
    Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made. Dec 3, 2019 at 9:12
  • 1
    Completely fails for n≤0 Dec 3, 2019 at 9:18
  • 1
    Add comments, explain your answer, read how to answer.
    – Aksen P
    Dec 3, 2019 at 9:29
-1

This worked for me -

My code:

#include <iostream>
using namespace std;

int main()
{
    int n = 32;
    string s = to_string(n);
    cout << "string: " + s  << endl;
    return 0;
}
2
  • @PeterMortensen Look carefully, it's std::to_string()
    – Kotauskas
    Jul 3, 2019 at 11:15
  • i have using namespace std; :) Jan 21, 2020 at 15:18
-2

I think using stringstream is pretty easy:

 string toString(int n)
 {
     stringstream ss(n);
     ss << n;
     return ss.str();
 }

 int main()
 {
    int n;
    cin >> n;
    cout << toString(n) << endl;
    return 0;
 }
1
  • This was mentioned in the question, and unfortunately is quite slow.
    – Kotauskas
    Jul 3, 2019 at 10:58
-3
char * bufSecs = new char[32];
char * bufMs = new char[32];
sprintf(bufSecs, "%d", timeStart.elapsed()/1000);
sprintf(bufMs, "%d", timeStart.elapsed()%1000);
2
  • 13
    leaks memory, -1 from me
    – paulm
    Mar 25, 2015 at 16:27
  • 1
    This smells like buffer overrun.
    – Kotauskas
    Jul 3, 2019 at 10:49
-3
namespace std
{
    inline string to_string(int _Val)
    {   // Convert long long to string
        char _Buf[2 * _MAX_INT_DIG];
        snprintf(_Buf, "%d", _Val);
        return (string(_Buf));
    }
}

You can now use to_string(5).

2
  • 11
    While this solution works, it is highly discouraged! Names starting with underscore and a capital letter are reserved for the compiler, you shouldn't ever use them. Injecting functions into the std namespace is not something you should ever do, either. Also, it doesn't seem like _MAX_INT_DIG is a standard macro, so if it is defined wrongly, this code has the great potential of inducing undefined behaviour. -1
    – iFreilicht
    Nov 19, 2014 at 14:57
  • 7
    What is _MAX_INT_DIG and why is it doubled?
    – paulm
    Mar 25, 2015 at 16:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.