65.9K
CodeProject is changing. Read more.
Home

C++: Converting an MFC CString to a std::string

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.90/5 (19 votes)

Mar 30, 2011

CPOL
viewsIcon

74123

As you use CString, you have access to the CW2A ATL macro, assuming that you work with _UNICODE defined, here it is:CString theCStr;...std::string STDStr( CW2A( theCStr.GetString() ) );which will convert to "system encoding on the user's machine" (thanks Nemanja[^] comment !), if you...

As you use CString, you have access to the CW2A ATL macro, assuming that you work with _UNICODE defined, here it is:
CString theCStr;
...
std::string STDStr( CW2A( theCStr.GetString() ) );
which will convert to "system encoding on the user's machine" (thanks Nemanja[^] comment !), if you want to specify the code page, UTF-8 encoded for example as the original, here it is:
CString theCStr;
...
std::string STDStr( CW2A( theCStr.GetString(), CP_UTF8 ) );
:)