Click here to Skip to main content
Full site     10M members (39.4K online)    

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

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 ) );
:)
 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
General5!
Pranit Kothari
13 Dec '11 - 1:27 
GeneralReason for my vote of 5 Nice,good and not complicated
Pranit Kothari
13 Dec '11 - 1:26 
GeneralReason for my vote of 5 simple way to accomplish the task...
Albert Holguin
2 Apr '11 - 18:48 
GeneralReason for my vote of 5 Nice. However, be aware that your ap...
Nemanja Trifunovic
30 Mar '11 - 8:12 

Last Updated 2 Apr 2011 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013