CString、LPSTR、std::string、LPCSTR之间的转换

来源:互联网 发布:乐乎宝山老来俏 编辑:程序博客网 时间:2024/06/05 04:01
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
=newchar[aLen];
    WideCharToMultiByte(CP_ACP,
0,strCS,wLen,lpa,aLen,NULL,NULL);
   
return lpa;
}


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


LPOLESTR MBCS2WideChar( LPCSTR lpa )
{
    size_t aLen
= strlen(lpa)+1;
   
int wLen= MultiByteToWideChar(CP_ACP,0,lpa,aLen,NULL,0);
    LPOLESTR lpw
=new WCHAR[wLen];
    MultiByteToWideChar(CP_ACP,
0,lpa,aLen,lpw,wLen);
   
return lpw;
}


CString MBCS2CString( LPCSTR lpa )
{
    LPOLESTR lpw
= MBCS2WideChar(lpa);
    CString cstring(lpw);
    delete [] lpw;
   
return  cstring;
}


CString StdStr2CSting(
const std::string& stdStr )
{
   
return MBCS2CString(stdStr.c_str());
}
原创粉丝点击