版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/t60339/article/details/76576196
ANSI与UTF-8编码互相转换
std::string EncodeConvert(std::string sInfo, int sourceEncode, int targetEncode)
{
// convert unicode
const char* buf = sInfo.c_str();
int len = MultiByteToWideChar(sourceEncode, 0, buf, -1, NULL, 0);
std::vector<wchar_t> unicode(len);
MultiByteToWideChar(sourceEncode, 0, buf, -1, &unicode[0], len);
wstring wstr(&unicode[0]);
// convert MultiByte
const wchar_t *wbuf =wstr.c_str();
len = WideCharToMultiByte(targetEncode, 0, wbuf, -1, NULL, 0, NULL, NULL);
std::vector<char> multi(len);
WideCharToMultiByte(targetEncode, 0, wbuf, -1, &multi[0], len, NULL, NULL);
string sMulti(&multi[0]);
return sMulti;
}
ANSI->UTF8
EncodeConvert(sText, CP_ACP, CP_UTF8);
UTF8->ANSI
EncodeConvert(sText, CP_UTF8, CP_ACP);
CString,std::string,char*,const char*互相转换
CString cstr;
std::string stdstr;
char* pChar;
const char* pcChar;
CString->std::string
stdstr = (LPSTR)(LPCTSTR)cstr;
stdstr = cstr.GetBuffer(cstr.GetLength());
CString->char*
pChar = (LPSTR)(LPCTSTR)cstr;
CString->const char*
pcChar = (LPCTSTR)cstr;
std::string->CString
cstr = stdstr.c_str();