字符集转换: Unicode

   字符集转换: Unicode - Ansi

 1 string UnicodeToAnsi ( const wstring& wstrSrc )
 2 {
 3     /*!< 分配目标空间, 一个16位Unicode字符最多可以转为4个字节*/
 4     //int iAllocSize = static_cast<int>( wstrSrc.size() * 4 + 10 );
 5     int iAllocSize = WideCharToMultiByte(CP_THREAD_ACP,0,wstrSrc.c_str(),-1,NULL,0,NULL,NULL);
 6     char* pwszBuffer = new char[ (UINT)iAllocSize ];
 7     memset(pwszBuffer,0,iAllocSize);
 8     if ( NULL == pwszBuffer )
 9     {
10         return "";
11     }    
12     int iCharsRet = WideCharToMultiByte( CP_THREAD_ACP, 0, wstrSrc.c_str(), -1,pwszBuffer, iAllocSize, NULL, NULL );
13     /*!< 成功 */
14     string strRet;
15     if ( 0 < iCharsRet )
16     {
17         (void)strRet.assign ( pwszBuffer, static_cast<size_t>( iCharsRet ) );
18     }
19 
20     /*!< 释放内存 */
21     delete[] pwszBuffer;
22 
23     return strRet;
24 }
原文地址:https://www.cnblogs.com/hwm520hlf1314/p/3457007.html