Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / MFC
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
30 Oct 2011CPOL 28.2K   2   5
In UNICODE:CString str = L"Test";std::wstring ws(str);std::string s;s.assign(ws.begin(), ws.end());
In UNICODE:

CString str = L"Test";
std::wstring ws(str);
std::string s;

s.assign(ws.begin(), ws.end());

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Robotz Software
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThis conversion is bad! Pin
Theo Buys1-Nov-13 5:29
Theo Buys1-Nov-13 5:29 
Questionconvert string to Cstring? Pin
Vijay Kumbhani29-Oct-13 3:05
Vijay Kumbhani29-Oct-13 3:05 
AnswerRe: convert string to Cstring? Pin
steveb3-Nov-13 6:57
mvesteveb3-Nov-13 6:57 
BugNot a unicode aware transformation Pin
pasztorpisti16-Aug-12 11:52
pasztorpisti16-Aug-12 11:52 
GeneralI don't think it would give the desired result if the origin... Pin
Philippe Mori25-Oct-11 13:20
Philippe Mori25-Oct-11 13:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.