16,471
社区成员
发帖
与我相关
我的任务
分享
// 功能: WideChar字符串转换为ANSI
// 参数:[in]szWidechar - 被转换的WideChar字符串
// 返回值: char* 转换后的ANSI字符串指针,调用者需要delete
char* CUtility::WideCharToANSI( const wchar_t* szWidechar )
{
ASSERT(szWidechar != NULL);
int widecharLen = ::WideCharToMultiByte(CP_ACP, 0, szWidechar, -1, NULL, 0, NULL, NULL);
char* pAnsi = new char[widecharLen+1];
memset(pAnsi, 0x00, widecharLen+1);
::WideCharToMultiByte(CP_ACP, 0, szWidechar, -1, pAnsi, widecharLen, NULL, NULL);
return pAnsi;
}
UNICODE下CString转换成char*的方法
方法1: CString str = _T("测试用例");
int nLength = WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL);
char *pBuffer = new char[nLength + 1];
WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),pBuffer,nLength,NULL,NULL);
pBuffer[nLength] = 0;
方法2:
CString s = _T("测试用例");
USES_CONVERSION;
char* lpsz = T2A(s.GetBuffer(s.GetLength()));
s.ReleaseBuffer(s.GetLength());