c++ ANSI、UNICODE、UTF8互转 staticstd::wstringMBytesToWString(constchar*lpcszString);
staticstd::stringWStringToMBytes(constwchar_t*lpwcszWString);
staticstd::wstringUTF8ToWString(constchar*lpcszString);
staticstd::stringWStringToUTF8(constwchar_t*lpwcszWString);
std::wstringKKLogObject::MBytesToWString(constchar*lpcszString)
{
intlen=strlen(lpcszString);
intunicodeLen=::MultiByteToWideChar(CP_ACP,0,lpcszString,-1,NULL,0);
wchar_t*pUnicode=newwchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar(CP_ACP,0,lpcszString,-1,(LPWSTR)pUnicode,unicodeLen);
wstringwString=(wchar_t*)pUnicode;
delete[]pUnicode;
returnwString;
}
std::stringKKLogObject::WStringToMBytes(constwchar_t*lpwcszWString)
{
char*pElementText;
intiTextLen;
//widechartomultichar
iTextLen=::WideCharToMultiByte(CP_ACP,0,lpwcszWString,-1,NULL,0,NULL,NULL);
pElementText=newchar[iTextLen+1];
memset((void*)pElementText,0,(iTextLen+1)*sizeof(char));
::WideCharToMultiByte(CP_ACP,0,lpwcszWString,0,pElementText,iTextLen,NULL,NULL);
std::stringstrReturn(pElementText);
delete[]pElementText;
returnstrReturn;
}
std::wstringKKLogObject::UTF8ToWString(constchar*lpcszString)
{
intlen=strlen(lpcszString);
intunicodeLen=::MultiByteToWideChar(CP_UTF8,0,lpcszString,-1,NULL,0);
wchar_t*pUnicode;
pUnicode=newwchar_t[unicodeLen+1];
memset((void*)pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar(CP_UTF8,0,lpcszString,-1,(LPWSTR)pUnicode,unicodeLen);
wstringwstrReturn(pUnicode);
delete[]pUnicode;
returnwstrReturn;
}
std::stringKKLogObject::WStringToUTF8(constwchar_t*lpwcszWString)
{
char*pElementText;
intiTextLen=::WideCharToMultiByte(CP_UTF8,0,(LPWSTR)lpwcszWString,-1,NULL,0,NULL,NULL);
pElementText=newchar[iTextLen+1];
memset((void*)pElementText,0,(iTextLen+1)*sizeof(char));
::WideCharToMultiByte(CP_UTF8,0,(LPWSTR)lpwcszWString,-1,pElementText,iTextLen,NULL,NULL);
std::stringstrReturn(pElementText);
delete[]pElementText;
returnstrReturn;
}