Convert CString to std::wstring
Asked Answered
U

4

17

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

Uncomfortable answered 11/1, 2010 at 10:44 Comment(2)
Please see this: #258550Ambiversion
and #859804Librettist
C
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());
Cue answered 11/1, 2010 at 10:52 Comment(0)
R
2

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

CString s;
std::wstring s1 = s;
Romilly answered 11/1, 2010 at 10:48 Comment(0)
A
1

Try this:

std::wstring strString((LPCTSTR)strCString);
Ambie answered 11/1, 2010 at 10:51 Comment(1)
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.Usk
N
0
CString s = _T("Привет");
USES_CONVERSION;
std::wstring ws(A2W((LPCTSTR)s));
Nittygritty answered 11/3, 2019 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.