Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C++: Converting an MFC CString to a std::string

0.00/5 (No votes)
1 Dec 2011 1  
LPSTR WideChar2MBCS( const CString& strCS ){ const UINT wLen = strCS.GetLength() + 1; UINT aLen = WideCharToMultiByte(CP_ACP,0,strCS,wLen,NULL,0,NULL,NULL); LPSTR lpa = new char[aLen]; WideCharToMultiByte(CP_ACP,0,strCS,wLen,lpa,aLen,NULL,NULL); return...
C++
LPSTR WideChar2MBCS( const CString& strCS )
{
    const UINT wLen = strCS.GetLength() + 1;
    UINT aLen = WideCharToMultiByte(CP_ACP,0,strCS,wLen,NULL,0,NULL,NULL);
    LPSTR lpa = new char[aLen];
    WideCharToMultiByte(CP_ACP,0,strCS,wLen,lpa,aLen,NULL,NULL);
    return lpa;
}

std::string WideChar2StdStr(const CString& strcs)
{
    LPSTR lpa = WideChar2MBCS(strcs);
    std::string stdStr(lpa);
    delete [] lpa;
    return stdStr;
}

I hope this will be useful.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here