自是人生长恨水长东

C++ C# PERL ERLANG C MS LINUX

C++博客 首页 新随笔 联系 聚合 管理
  13 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

//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
}

 

posted on 2010-10-27 16:59 傅红血 阅读(375) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理