MFC格式转换 UTF8 ANSI UNICODE
函数拿起来就可以用
参数说明:sChartSet : FromANSI(ANSI->UNICODE) , ToANSI (UNICODE->ANSI) , FromUTF8 (UTF8->UNICODE) , ToUTF8 (UNICODE->UTF8)
CString CSqlConTestDlg::UnicodeCovert(CString sSourceStr , CString sCharSet)
{
bool bToUnicode = true;
if(!strnicmp(sCharSet,"To",2))
{
sCharSet = sCharSet.Mid(2);
bToUnicode = FALSE;
}
else if(!strnicmp(sCharSet,"From",4))
{
sCharSet = sCharSet.Mid(4);
}
else
{
return "";
}
UINT nCodePage = CP_ACP;
if(stricmp(sCharSet,"ANSI") == 0 || stricmp(sCharSet,"ACP") == 0)
nCodePage = CP_ACP; //ANSI translation
else if(stricmp(sCharSet,"UTF8") == 0)
nCodePage = CP_UTF8; //UTF8 translation
else
return "";
CString sCovert;
if(bToUnicode) //->Unicode
{
DWORD nWideBuf = MultiByteToWideChar(nCodePage , 0 , (LPCTSTR)sSourceStr , sSourceStr.GetLength(),NULL,0); //探测转成Unicode的长度
sCovert.GetBufferSetLength(nWideBuf*2);
MultiByteToWideChar(nCodePage,0,(LPCTSTR)sSourceStr,sSourceStr.GetLength(),(LPWSTR)LPCTSTR(sCovert),nWideBuf);
}
else //Unicode - >
{
DWORD nWideCount = (sSourceStr.GetLength() + 1) * 2;
if(nWideCount == 0)
sCovert.Empty();
else
{
int nMultilen = WideCharToMultiByte(nCodePage, 0 , (LPWSTR)LPCTSTR(sSourceStr),nWideCount,sCovert.GetBufferSetLength(nWideCount),0);
sCovert.GetBufferSetLength(nMultilen);
}
}
return sCovert;
}