CString->LPTSTR的转化可以用函数GetBuff()
- CString StrInfo="C:\\Windows";
- LPTSTR StrTest=StrInfo.GetBuffer(20);
- strcpy(StrTest,"NewString");
- StrInfo.ReleaseBuffer();
- MessageBox(StrInfo,"信息提示",MB_OK);
CString->TCHAR*的转化可以用函数GetBuff()
函数原型为:LPTSTR GetBuffer( int nMinBufLength );
- CString str("CString");
- TCHAR* szMsg = new TCHAR[100];
- //其参数为CString字符串的长度
- szMsg = str.GetBuffer(str.GetLength());
- str.ReleaseBuffer();
- delete []szMsg;
- szMsg = NULL;
TCHAR*->CString的转化
- TCHAR szTchar[18] = L"TCHAR";
- CString str;
- str.Format(_T("%s"),szTchar);
- //////////////////////////////
- /*
- ***********************************************************************
- * 函数: TransCStringToTCHAR
- * 描述:将CString 转换为 TCHAR*
- * 日期:2003-05-30 King
- ***********************************************************************
- */
- TCHAR* CPublic::CString2TCHAR(CString &str)
- {
- int iLen = str.GetLength();
- TCHAR* szRs = new TCHAR[iLen];
- lstrcpy(szRs, str.GetBuffer(iLen));
- str.ReleaseBuffer();
- return szRs;
- }
- /*
- ***********************************************************************
- * 函数: THCAR2Char
- * 描述:将TCHAR* 转换为 char*
- * 日期:2003-05-30 King
- ***********************************************************************
- */
- char* CPublic::THCAR2char(TCHAR* tchStr)
- {
- int iLen = 2*wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度
- char* chRtn = new char[iLen+1]
- wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值
- return chRtn;
- }
- /*
- CString、TCHAR*、char*转换
- ***********************************************************************
- * 函数: CString2char
- * 描述:将CString转换为 char*
- * 日期:2003-05-26 King
- ***********************************************************************
- */
- char* CPublic::CString2char(CString &str)
- {
- int len = str.GetLength();
- char* chRtn = (char*)malloc((len*2+1)*sizeof(char));//CString的长度中汉字算一个长度
- memset(chRtn, 0, 2*len+1);
- USES_CONVERSION;
- strcpy((LPSTR)chRtn,OLE2A(str.LockBuffer()));
- return chRtn;
- }
- //参考
- ///////////////////////
- //Pocket PC上的UNICODE和ANSI字符串
- //By Vassili Philippov, September 26, 2001.
- //杨方思歧 译
- ////////////////////////
- /*
- ***********************************************************************
- * 函 数 名:GetAnsiString
- * 描 述:将CString(unicode)转换为char*(ANSI)
- * 参 数:CString &s 要转换的CString
- * 返 回 值:返回转换结果
- * 日期、作者:创建日期:2003-5-30 King
- * 最后修改:2003-5-30 King
- ***********************************************************************
- */
- char* GetAnsiString(const CString &s)
- {
- int nSize = 2*s.GetLength();
- char *pAnsiString = new char[nSize+1];
- wcstombs(pAnsiString, s, nSize+1);
- return pAnsiString;
- }
- char szA[100]; //An ANSI string buffer
- WCHAR szW[100]; //A Unicode string buffer
- //Normal sprintf:all strings are ANSI
- sprintf(szA, "%s","ANSI Str");
- //Converts Unicode string to ANSI
- sprintf(szA,"%S",L"Unicode Str");
- //Normal swprintf:all strings are Unicode
- swprintf(szW,L"%s",L"Unicode Str");
- //Converts ANSI string to Unicode
- swprintf(szW,L"%S", "ANSI Str");