CString、char、string、int、_bstr_t、CTime、COleDateTime相互转换&判断一个字符串是一个浮点数

#include using namespace std; #include { 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; //CString和string的互相转换 CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); std::string->CString 例子: CString strMfc; std::string strStl=“test“; strMfc=strStl.c_str(); 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("浮点"); } } // 各种数据类型转换为std::string template<typename T> std::string to_string(T const & value) { std::ostringstream ost; ost << value; ost.flush(); return ost.str(); }

你可能感兴趣的:(CString、char、string、int、_bstr_t、CTime、COleDateTime相互转换&判断一个字符串是一个浮点数)