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

C++

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

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);

Solution 2 - C++

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.

Solution 3 - C++

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();
}

Solution 4 - C++

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

Solution 5 - C++

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

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

Solution 6 - C++

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

Solution 7 - C++

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.

Solution 8 - C++

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

Solution 9 - C++

   #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

Solution 10 - C++

Check out std::stringstream.

Solution 11 - C++

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.

Solution 12 - C++

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArnis LapsaView Question on Stackoverflow
Solution 1 - C++jichiView Answer on Stackoverflow
Solution 2 - C++SkurmedelView Answer on Stackoverflow
Solution 3 - C++Gordon FreemanView Answer on Stackoverflow
Solution 4 - C++Piotr FindeisenView Answer on Stackoverflow
Solution 5 - C++Yochai TimmerView Answer on Stackoverflow
Solution 6 - C++dreadwailView Answer on Stackoverflow
Solution 7 - C++beef2kView Answer on Stackoverflow
Solution 8 - C++Fred LarsonView Answer on Stackoverflow
Solution 9 - C++Martin BeckettView Answer on Stackoverflow
Solution 10 - C++DonView Answer on Stackoverflow
Solution 11 - C++Greg DomjanView Answer on Stackoverflow
Solution 12 - C++cmaxoView Answer on Stackoverflow