Convert Unicode CString to STL::string

Posted on 2006-04-03
Medium Priority
Last Modified: 2013-11-20
How to convert an Unicode CString to STL::string?

Question by:sparkythedog
  • 2
LVL 86

Expert Comment

ID: 16362765
If you need an ANSI string:

CString strUni = L"Test";

size_t len = strUni.GetLength() + 1;
char* psz = new char[len];

std::string str = psz;

delete [] psz;

Author Comment

ID: 16362811
Hi there,
thanks for your reply

I need the STL string to contain the UNICODE string from CString.

LVL 86

Accepted Solution

jkr earned 800 total points
ID: 16362841
No problem:

CString strUni = L"Test";
std::wstring wstr = (LPCTSTR) strUni;

'wstring' is the UNICODE version of a STL string.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

621 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