Click here to Skip to main content
15,885,164 members
Articles / Desktop Programming / MFC
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Dec 2011CPOL 7K   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --