17

How can I convert from CString to std::wstring?

2

4 Answers 4

28

To convert CString to std::wstring:

CString hi("Hi"); std::wstring hi2(hi); 

And to go the other way, use c_str():

std::wstring hi(L"Hi"); CString hi2(hi.c_str()); 
Sign up to request clarification or add additional context in comments.

Comments

2

This should work as CString has operator LPCTSTR() defined:

CString s; std::wstring s1 = s; 

Comments

1

Try this:

std::wstring strString((LPCTSTR)strCString); 

1 Comment

Why use a C cast for that? A fellow-worker of mine once was in the position that he had to find explicit casts, as some of them didn't work on the platform he needed to port a 4MLoC project to. He praised everyone who used C++' explicit casts (you can grep for them) and fought hard to ban all C-style casts, since they were so hard to find.
0
CString s = _T("Привет"); USES_CONVERSION; std::wstring ws(A2W((LPCTSTR)s)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.