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 see开发者_开发技巧n 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#.
It's as simple as:
long myLong=0;
CString s;
// Your one line solution is below
s.Format("%ld",myLong);
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
精彩评论