将CString 转换为 TCHAR*
TCHAR* CString2TCHAR(CString &str)
{
int iLen = str.GetLength();
TCHAR* szRs = new TCHAR[iLen];
lstrcpy(szRs, str.GetBuffer(iLen));
str.ReleaseBuffer();
return szRs;
}
将TCHAR* 转换为 char*
char* TCHAR2char(TCHAR* tchStr)
{
int iLen = 2wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度
char chRtn = new char[iLen+1]
wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值
return chRtn;
}
将 char* 转换为 TCHAR*
TCHAR *char2tchar(char *str)
{
int iLen = strlen(str);
TCHAR *chRtn = new TCHAR[iLen+1];
mbstowcs(chRtn, str, iLen+1);
return chRtn;
}
将CString转换为 char*
char* CString2char(CString &str)
{
int len = str.GetLength();
char* chRtn = (char)malloc((len2+1)sizeof(char));//CString的长度中汉字算一个长度
memset(chRtn, 0, 2len+1);
USES_CONVERSION;
strcpy((LPSTR)chRtn,OLE2A(str.LockBuffer()));
return chRtn;
}
将CString(unicode)转换为char*(ANSI)
参 数:CString &s 要转换的CString
char* GetAnsiString(const CString &s)
{
int nSize = 2*s.GetLength();
char *pAnsiString = new char[nSize+1];
wcstombs(pAnsiString, s, nSize+1);
return pAnsiString;
}