I have a std::string
.
I need to convert this std:string
to a Cstring
.
I try to use the .c_str()
but it's only for non-unicode project and i use unicode project ( because non unicode project are depreceated wiht VS2013).
Anyone could show my how to convert an std::string
to a CString
in unicode project ?
Unicode
CString
's constructor acceptschar*
so you can do this:Use the ATL conversion macros. They work in every case, when you use CString. CString is either MBCS or Unicode... depends on your Compiler Settingss.
Bonus: if you use the conversion frequently you can define a macro:
so your code is more readable.
CString
has a conversion constructor taking aconst char*
(CStringT::CStringT
). Converting astd::string
to aCString
is as simple as:This works for both UNICODE and MBCS projects. If your
std::string
contains embeddedNUL
characters you have to use a conversion constructor with a length argument:Note that the conversion constructors implicitly use the ANSI code page of the current thread (
CP_THREAD_ACP
) to convert between ANSI and UTF-16 encoding. If you cannot (or do not want to) change the thread's ANSI code page, but still need to specify an explicit code page to use for the conversion, you have to use another solution (e.g. the ATL and MFC String Conversion Macros).