Solved

CString to wstring

Posted on 2007-11-26
5
4,225 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now