- #include <******.h>
- using namespace std;
- void main()
- {
- CString strCString="ABC";
- char strchar[256],*pstr;
- pstr=(LPSTR)(LPCTSTR)strCString; //CString---->char*
- strcpy(strchar,(LPSTR)(LPCTSTR)strCString); //CString---->char[]
- _bstr_t strbstr=pstr; //char*---->_bstr_t
- WCHAR *strWCHAR=strbstr; //b_str_t--->UNICODE
- strbstr=strWCHAR;
- pstr=strbstr; //UNICODE---->char*
- strCString="10";
- int istr=atoi((LPSTR)(LPCTSTR)strCString); //CString、char[]、char*------>int
- strCString.Format("%d",istr); //int----->CString
- sprintf(strchar,"%d",istr); //int----->char[]
- pstr=new char[256]; //字符串申请空间
- strcpy(pstr,"ABC"); //字符串赋值
- delete []pstr; //字符串释放
- string strstring="ABC";
- pstr=(char*)strstring.c_str(); //string---->char*
- strCString="2003-10-27 6:24:37"; //CString--->COleDateTime
- COleVariant vtime(strCString);
- vtime.ChangeType(VT_DATE);
- COleDateTime time4=vtime;
- COleDateTime time1(1977,4,16,2,2,2); //COleDataTime--->CTime
- SYSTEMTIME systime;
- VariantTimeToSystemTime(time1, &systime);
- CTime tm(systime);
- time_t time2=tm.GetTime(); //CTime--->time_t
- COleDateTime time3(time2); //time_t--->COleDateTime
- //判断字符串是否是某种类型
- CString sValue("123.1");
- COleVariant vValue(sValue);
- BOOL bStrIsFloat = (SUCCEEDED(VariantChangeType(&vValue, &vValue, 0, VT_R8)) && sValue.Find('.') != -1);
- if(bStrIsFloat)
- {
- AfxMessageBox("浮点");
- }
- }
- string 转 CString
- CString.format("%s", string.c_str());
- char 转 CString
- CString.format("%s", char*);
- char 转 string
- string s(char *);
- string 转 char *
- char *p = string.c_str();
- CString 转 string
- string s(CString.GetBuffer());
- GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
- CString互转int
- 将字符转换为整数,可以使用atoi、_atoi64或atol。
- 而将数字转换为CString变量,可以使用CString的Format函数。如
- CString s;
- int i = 64;
- s.Format("%d", i)
- CString互转char*
- ///char * TO CString
- CString strtest;
- char * charpoint;
- charpoint="give string a value";
- strtest=charpoint;
- (这个方法我在VisualStdio2005中试了一下,会出现错误.暂且保存下来,回来试试其他环境能不能通过.)
- 我自己还实验了一种方法:
- char* pCh = "kkkkkk";
- CString str(pCh);
- 就是把char*作为初始化CString的参数,经过实验可以正常使用.
- ///CString TO char *
- charpoint=strtest.GetBuffer(strtest.GetLength());
- 可以用CString.Format("%s",char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR)CString就可以了。
- CString转换 char[100]
- char a[100];
- CString str("aaaaaa");
- strcpy(a,(LPCTSTR)str,sizeof(a));
本文章来自于【http://xboyhjm.iteye.com/blog/889600】