Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
2 Apr 2011 1  
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 ) );

:)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here