//UTF8编码
void UTF8Encode(LPCTSTR lpInput, std::string &strOutput)
{
int nOut;
const WCHAR* pInput;
char* pTest;
if((0 == lpInput) || (0 == _tcslen(lpInput)))
{
strOutput.erase(strOutput.begin(), strOutput.end());
return;
}
#ifdef _UNICODE
pInput = lpInput;
#else
WCHAR* pBuffer;
nOut = MultiByteToWideChar(CP_ACP, 0, lpInput, -1, NULL, 0);
nOut++;
pBuffer = new WCHAR[nOut];
MultiByteToWideChar(CP_ACP, 0, lpInput, -1, pBuffer, nOut);
pInput = pBuffer;
#endif
nOut = WideCharToMultiByte(CP_UTF8, 0, pInput, -1, NULL, 0, NULL, NULL);
nOut++;
pTest = new char[nOut];
WideCharToMultiByte(CP_UTF8, 0, pInput, -1, pTest, nOut, NULL, NULL);
strOutput = pTest;
delete []pTest;
#ifndef _UNICODE
delete []pBuffer;
#endif
}
//UTF8解码
void UTF8Decode(const char *pInput, tstring &strOutput)
{
int nOut;
WCHAR* pBuffer;
if((0 == pInput) || (0 == strlen(pInput)))
{
strOutput.erase(strOutput.begin(), strOutput.end());
return;
}
nOut = MultiByteToWideChar(CP_UTF8, 0, pInput, -1, NULL, 0);
nOut++;
pBuffer = new WCHAR[nOut];
MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pBuffer, nOut);
#ifdef _UNICODE
strOutput = pBuffer;
delete []pBuffer;
#else
char* pTest;
nOut = WideCharToMultiByte(CP_ACP, 0, pBuffer, -1, NULL, 0, NULL, NULL);
nOut++;
pTest = new char[nOut];
nOut = WideCharToMultiByte(CP_ACP, 0, pBuffer, -1, pTest, nOut, NULL, NULL);
strOutput = pTest;
delete []pTest;
delete []pBuffer;
#endif
}