字符集转换: Unicode - Ansi

时间:2023-11-20 19:14:50

字符集转换: Unicode - Ansi

 string UnicodeToAnsi ( const wstring& wstrSrc )
{
/*!< 分配目标空间, 一个16位Unicode字符最多可以转为4个字节*/
//int iAllocSize = static_cast<int>( wstrSrc.size() * 4 + 10 );
int iAllocSize = WideCharToMultiByte(CP_THREAD_ACP,,wstrSrc.c_str(),-,NULL,,NULL,NULL);
char* pwszBuffer = new char[ (UINT)iAllocSize ];
memset(pwszBuffer,,iAllocSize);
if ( NULL == pwszBuffer )
{
return "";
}
int iCharsRet = WideCharToMultiByte( CP_THREAD_ACP, , wstrSrc.c_str(), -,pwszBuffer, iAllocSize, NULL, NULL );
/*!< 成功 */
string strRet;
if ( < iCharsRet )
{
(void)strRet.assign ( pwszBuffer, static_cast<size_t>( iCharsRet ) );
} /*!< 释放内存 */
delete[] pwszBuffer; return strRet;
}