CString to wstring

Hi,
I need to convert a CString to a wstring. Actual I do it while initialize the wstring (std::wstring path(sTempBuffer);) , but now I need to do it in a function. So how to convert a CString to a allway initialized wstring?

Ingo
OcranaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkrConnect With a Mentor Commented:
If you have UNICODE set, just use

CString test;
wstring wstr = (LPCTSTR) test;
0
 
Jaime OlivaresSoftware ArchitectCommented:
have a look to this great article:
http://www.codeproject.com/string/cppstringguide2.asp
0
 
OcranaAuthor Commented:
sorry cannot see the cstring to wstring, only wstring to cstring.
0
 
Jaime OlivaresSoftware ArchitectCommented:
there is a wstring constructor that accepts 'const char *'
CString is equivalent to LPCTSTR, which is the same as 'const char *'

so, you should be able to use

CString a = "test"';
std::wstring st(a);
 
0
 
jkrCommented:
On, and if it is an ANSI CString, make that

#include <ios>
using namespace std;

CString test;
string tmp = (LPCTSTR) test;
wstring wstr = widen(tmp.begin(),tmp.end());
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.