Click here to Skip to main content
Click here to Skip to main content

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

By , 2 Apr 2011
 
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

jean Davy
Engineer
France France
Member
I'm an independent Software Developer living in the Alpes Maritimes (aka French Riviera).
 
Even if in the past I have worked regularily with Fortran, Pascal, Basic (not necessarily Visual), and much more esoteric languages (who knows Prolog, Lotus 123 macros ?), even if I still work from time to time with C#, Java or some "interpreted languages", I'm more specialized in C++.
I work on MFC since the very first version, I enjoy working on Visual Studio 2010, its C++0x features, STL, MFC, boost, WTL ...

Comments and Discussions

Comment 4 messages have been posted for this article Visit http://www.codeproject.com/Tips/175043/C-Converting-an-MFC-CString-to-a-std-string to post and view comments on this article, or click here to get a print view with messages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 2 Apr 2011
Article Copyright 2011 by jean Davy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use