?
Solved

Convert Unicode CString to STL::string

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

Thanks
0
Comment
Question by:sparkythedog
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16362765
If you need an ANSI string:

CString strUni = L"Test";

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

std::string str = psz;

delete [] psz;
0
 

Author Comment

by:sparkythedog
ID: 16362811
Hi there,
thanks for your reply

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

Thanks
0
 
LVL 86

Accepted Solution

by:
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.
0

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