Solved

CString to wstring

Posted on 2007-11-26
5
4,396 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:Ocrana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20353155
have a look to this great article:
http://www.codeproject.com/string/cppstringguide2.asp
0
 

Author Comment

by:Ocrana
ID: 20353333
sorry cannot see the cstring to wstring, only wstring to cstring.
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 20353467
If you have UNICODE set, just use

CString test;
wstring wstr = (LPCTSTR) test;
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20353487
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
 
LVL 86

Expert Comment

by:jkr
ID: 20353495
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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question