Convert Unicode CString to STL::string

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

Question by:sparkythedog
    LVL 86

    Expert Comment

    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

    Hi there,
    thanks for your reply

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

    LVL 86

    Accepted Solution

    No problem:

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

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

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Suggested Solutions

    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    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.
    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.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 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

    17 Experts available now in Live!

    Get 1:1 Help Now