65.9K
CodeProject is changing. Read more.
Home

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

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Dec 1, 2011

CPOL
viewsIcon

7150

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...

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.