Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

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

, 1 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

stonexin
Web Developer
China China
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by stonexin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid