Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an Int to a CString?

I can convert a Double to a CString using _ecvt

result_str=_ecvt(int,15,&decimal,&sign); 

So, is there a method like the one above that converts an int to CString?

like image 644
Eslam Mohamed Mohamed Avatar asked Sep 26 '12 13:09

Eslam Mohamed Mohamed


People also ask

How do you convert int to CString in MFC?

Solution 3. CString MyString; int MyInt; MyString. Format(L"%d",MyInt); Another way is to use the std library's to_wstring[^], and then cast the result to CString.

How do you convert STD string to CString?

Converting a std::string to a CString is as simple as: std::string stdstr("foo"); CString cstr(stdstr. c_str()); This works for both UNICODE and MBCS projects.


2 Answers

Here's one way:

CString str; str.Format("%d", 5); 

In your case, try _T("%d") or L"%d" rather than "%d"

like image 162
dsgriffin Avatar answered Oct 08 '22 23:10

dsgriffin


If you want something more similar to your example try _itot_s. On Microsoft compilers _itot_s points to _itoa_s or _itow_s depending on your Unicode setting:

CString str; _itot_s( 15, str.GetBufferSetLength( 40 ), 40, 10 ); str.ReleaseBuffer(); 

it should be slightly faster since it doesn't need to parse an input format.

like image 35
snowdude Avatar answered Oct 08 '22 23:10

snowdude