GB2312和 UTF8的互相转换函数

  1. //将UTF8转化为GB2312  
  2. string  UTF8ToGB2132(string  strSrc)  
  3. {  
  4.     string result;  
  5.     WCHAR *wstrSrc = NULL;  
  6.     char *szRes = NULL;  
  7.     int i;  
  8.   
  9.     // UTF8转换成Unicode  
  10.     i = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, NULL, 0);  
  11.     wstrSrc = new WCHAR[i+1];  
  12.     MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, wstrSrc, i);  
  13.   
  14.     // Unicode转换成GB2312  
  15.     i = WideCharToMultiByte(CP_ACP, 0, wstrSrc, -1, NULL, 0, NULL, NULL);  
  16.     szRes = new char[i+1];  
  17.     WideCharToMultiByte(CP_ACP, 0, wstrSrc, -1, szRes, i, NULL, NULL);  
  18.   
  19.     result = string (szRes);  
  20.     if (wstrSrc != NULL)  
  21.     {  
  22.         delete []wstrSrc;  
  23.         wstrSrc = NULL;  
  24.     }  
  25.     if (szRes != NULL)  
  26.     {  
  27.         delete []szRes;  
  28.         szRes = NULL;  
  29.     }  
  30.   
  31.     return result;  
  32. }  出处:http://blog.csdn.net/tangaowen/article/details/6158984
posted @ 2013-07-28 20:03  stma  阅读(377)  评论(0)    收藏  举报