Arnis Lapsa
Arnis Lapsa

Reputation: 47577

How do I convert a long to a string in C++?

How do I convert a long to a string in C++?

Upvotes: 80

Views: 198008

Answers (12)

jichi
jichi

Reputation: 6623

In C++11, there are actually std::to_string and std::to_wstring functions in <string>.

string to_string(int val);
string to_string(long val);
string to_string(long long val);
string to_string(unsigned val);
string to_string(unsigned long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string (long double val);

Upvotes: 104

Yochai Timmer
Yochai Timmer

Reputation: 49251

You can use std::to_string in C++11

long val = 12345;
std::string my_val = std::to_string(val);

Upvotes: 20

cmaxo
cmaxo

Reputation: 31

The way I typically do it is with sprintf. So for a long you could do the following assuming that you are on a 32 bit architecture:

char buf[5] = {0}; // one extra byte for null    
sprintf(buf, "%l", var_for_long);

Upvotes: -1

dreadwail
dreadwail

Reputation: 15409

int main()
{
    long mylong = 123456789;
    string mystring;
    stringstream mystream;
    mystream << mylong;
    mystring = mystream.str();
    cout << mystring << "\n";
    return 0;
}

Upvotes: 13

Martin Beckett
Martin Beckett

Reputation: 96109

   #include <sstream>


   ....

    std::stringstream ss;
    ss << a_long_int;  // or any other type
    std::string result=ss.str();   // use .str() to get a string back

Upvotes: 4

Greg Domjan
Greg Domjan

Reputation: 14105

One of the things not covered by anybody so far, to help you think about the problem further, is what format should a long take when it is cast to a string.

Just have a look at a spreedsheet program (like Calc/Excel). Do you want it rounded to the nearest million, with brackets if it's negative, always to show the sign.... Is the number realy a representation of something else, should you show it in Oractal or Hex instead?

The answers so far have given you some default output, but perhaps not the right ones.

Upvotes: 3

Gordon Freeman
Gordon Freeman

Reputation: 1167

Well if you are fan of copy-paste, here it is:

#include <sstream>

template <class T>
inline std::string to_string (const T& t)
{
    std::stringstream ss;
    ss << t;
    return ss.str();
}

Upvotes: 29

beef2k
beef2k

Reputation: 2247

I don't know what kind of homework this is, but most probably the teacher doesn't want an answer where you just call a "magical" existing function (even though that's the recommended way to do it), but he wants to see if you can implement this by your own.

Back in the days, my teacher used to say something like "I want to see if you can program by yourself, not if you can find it in the system." Well, how wrong he was ;) ..

Anyway, if your teacher is the same, here is the hard way to do it..

std::string LongToString(long value)
{
  std::string output;
  std::string sign;

  if(value < 0)
  {
    sign + "-";
    value = -value;
  }

  while(output.empty() || (value > 0))
  {
    output.push_front(value % 10 + '0')
    value /= 10;
  }

  return sign + output;
}

You could argue that using std::string is not "the hard way", but I guess what counts in the actual agorithm.

Upvotes: 10

Piotr Findeisen
Piotr Findeisen

Reputation: 20720

boost::lexical_cast<std::string>(my_long) more here http://www.boost.org/doc/libs/1_39_0/libs/conversion/lexical_cast.htm

Upvotes: 20

Fred Larson
Fred Larson

Reputation: 62063

There are several ways. Read The String Formatters of Manor Farm for an in-depth comparison.

Upvotes: 5

Skurmedel
Skurmedel

Reputation: 22149

You could use stringstream.

#include <sstream>

// ...
std::string number;
std::stringstream strstream;
strstream << 1L;
strstream >> number;

There is usually some proprietary C functions in the standard library for your compiler that does it too. I prefer the more "portable" variants though.

The C way to do it would be with sprintf, but that is not very secure. In some libraries there is new versions like sprintf_s which protects against buffer overruns.

Upvotes: 61

Don
Don

Reputation: 777

Check out std::stringstream.

Upvotes: 2

Related Questions