Phil
Phil

Reputation: 3994

How to cast a LONG to a CString?

I want to cast a long to a cstring.

I've been struggling with this for a while and I've seen so many variants of solving this problem, more or less riddled with hassle and angst.

I know the question seems subjective, but it really shouldn't be in my opinion. There must be a way considered to be the best when the circumstances involve MFC and the standard libs that come with those circumstances.

I'm looking for a one-line solution that just works. Sort of like long.ToString() in C#.

Upvotes: 10

Views: 24308

Answers (2)

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74390

It's as simple as:

long myLong=0;
CString s;

// Your one line solution is below
s.Format("%ld",myLong);

Upvotes: 22

AJG85
AJG85

Reputation: 16197

There are many ways to do this:

CString str("");
long l(42);

str.Format("%ld", l); // 1
char buff[3];
_ltoa_s(l, buff, 3, 10); // 2
str = buff;
str = boost::lexical_cast<std::string>(l).c_str(); // 3
std::ostringstream oss;
oss << l; // 4
str = oss.str().c_str();
// etc

Upvotes: 6

Related Questions