1.CString s1="ad";
char s2[256];
strcpy(s2,s1);
2.strcpy(s2,s1.GetBuffer(s1.GetLength);
3.strcpy(s2,s1.GetBuffer(0);
4.for(int i=0;i<str.getlength();i++)
{
char[i]=str.get[i];
}
char[i]='/0';
5.CString 转char[]
char temp[]="2、如何将char数组里的内容copy到CString";
strcpy(m_chartocstring.GetBuffer(strlen(temp)),temp);
m_chartocstring.ReleaseBuffer();
AfxMessageBox("char数组转化为CString!是我亲自写的!",MB_OK);
UpdateData(0);
6.CString str1="PiggyXP";
char *str=new char[str.GetLength()+1];
strcpy(str,str1);
7.如果s1的长度太长会造成溢出,所以安全的方法是在使用strcpy()前先判断一下:
CString s1="ad";
char s2[256];
int len1 = s1.GetLength();
if(len1 <= 256)
strcpy(s2,s1.GetBuffer(0));
8.strcpy(s2,s1.GetBuffer(s1.GetLength);
s1.ReleaseBuffer();
9.strcpy(strchar,(LPSTR)(LPCTSTR)strCString); //CString---->char[]
10.如果copy的话,建议申明字符串用:
TCHAR* pBuf; // 或者用更好的LPTSTR和LPCTSTR,他们其实是TCHAR的指针
拷贝用 _tcscpy函数,他在ansi,unicode环境下都能很好工作,避免用strcpy.
CString str; // 目标字符串
int _toUse; // 需要使用的字符个数
LPTSTR tmp = str.GetBufferSetLength(_toUse); // 得到TCHAR*
// ... 使用
str.ReleaseBuffer(); // 使用完毕
11.见过一位前辈这样写的:
CString str = "Heloo";
char *chr = new char(str.getLength()+1);
memcpy(chr,str.getLength()+1,str.getLength()+1);
memcpy是不是比strcpy安全?
学习ing...
应该是memcpy(chr,str.GetBuffer(str.getLength()),str.getLength()+1);
12.一定要注意是否为UNICODE
CString s1="ad";
int len = s1.GetLength();
char s2[256];
memset((void*)s2,0,sizeof(char) * 256);
#ifdef _UNICODE
WideCharToMultiByte(CP_ACP,0,(LPCTSTR)s1,-1,s2,255,NULL,NULL);
#else
len = (len < 255)? len : 255;
strncpy(s2,(LPCTSTR)s1,len);
#endif
...