Unicode 下CString转char*
CString 转char*
有时候我们在Unicode编码下,会使用到 asc编码。在编程的时候我们会经常使用CString转到各种数据类型,并且,在转换的时候还需要注意编码方式。这里我把转换过程贴出来。
char* UnicodeToAsc(const wchar_t* szStr)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL);
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen];
WideCharToMultiByte(CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL);
return pResult;
}
char* CStringTochar(const CString str)
{
TCHAR Tchar[100];
wsprintf(Tchar, TEXT("%s"), str);
return UnicodeToAsc(Tchar);
}
//这里写一个调用实例
void main()
{
char *test = null;
CString str = _T("你好!");
test = CStringTochar(str);
}