Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#.

like image 362
Phil Avatar asked Sep 23 '11 15:09

Phil


People also ask

How do you convert long long int to string?

_ltoa() — Convert Long Integer to String h> char *_ltoa(long value, char *string, int radix); Note: The _ltoa function is supported only for C++, not for C.

What do you use for CString?

To use CString , include the atlstr. h header. The CString , CStringA , and CStringW classes are specializations of a class template called CStringT based on the type of character data they support. A CStringW object contains the wchar_t type and supports Unicode strings.

What is the header file for CString?

The cstring header file contains definitions for C++ for manipulating several kinds of strings. Include the standard header into a C++ program to effectively include the standard header <string. h> within the std namespace.

What does CString mean in C++?

cstring is the header file required for string functions. This function Returns a pointer to the last occurrence of a character in a string.


1 Answers

It's as simple as:

long myLong=0;
CString s;

// Your one line solution is below
s.Format("%ld",myLong);
like image 177
Michael Goldshteyn Avatar answered Sep 29 '22 14:09

Michael Goldshteyn