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);
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();
对有关数据类型转换的整理
int i = 100; long l = 2001; float f=300.2; double d=12345.119; char username[]="程佩君"; char temp[200]; char *buf; CString str; _variant_t v1; _bstr_t v2;
一、其它数据类型转换为字符串
- 短整型(int)
itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制 itoa(i,temp,2); ///按二进制方式转换 - 长整型(long)
ltoa(l,temp,10); - 浮点数(float,double)
用fcvt可以完成转换,这是MSDN中的例子: int decimal, sign; char *buffer; double source = 3.1415926535; buffer = _fcvt( source, 7, &decimal, &sign ); 运行结果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0 decimal表示小数点的位置,sign表示符号:0为正数,1为负数 - CString变量
str = "2008北京奥运"; buf = (LPSTR)(LPCTSTR)str; - BSTR变量
BSTR bstrValue = ::SysAllocString(L"程序员"); char * buf = _com_util::ConvertBSTRToString(bstrValue); SysFreeString(bstrValue); AfxMessageBox(buf); delete(buf); - CComBSTR变量
CComBSTR bstrVar("test"); char *buf = _com_util::ConvertBSTRToString(bstrVar.m_str); AfxMessageBox(buf); delete(buf); - _bstr_t变量
_bstr_t类型是对BSTR的封装,因为已经重载了=操作符,所以很容易使用 _bstr_t bstrVar("test"); const char *buf = bstrVar;///不要修改buf中的内容 AfxMessageBox(buf); - 通用方法(针对非COM数据类型)
用sprintf完成转换 char buffer[200]; char c = '1'; int i = 35; long j = 1000; float f = 1.7320534f; sprintf( buffer, "%c",c); sprintf( buffer, "%d",i); sprintf( buffer, "%d",j); sprintf( buffer, "%f",f);
二、字符串转换为其它数据类型 strcpy(temp,"123");
- 短整型(int)
i = atoi(temp); - 长整型(long)
l = atol(temp); - 浮点(double)
d = atof(temp); - CString变量
CString name = temp; - BSTR变量
BSTR bstrValue = ::SysAllocString(L"程序员"); ...///完成对bstrValue的使用 SysFreeString(bstrValue); - CComBSTR变量
CComBSTR类型变量可以直接赋值 CComBSTR bstrVar1("test"); CComBSTR bstrVar2(temp); - _bstr_t变量
_bstr_t类型的变量可以直接赋值 _bstr_t bstrVar1("test"); _bstr_t bstrVar2(temp);
三、其它数据类型转换到CString 使用CString的成员函数Format来转换,例如:
- 整数(int)
str.Format("%d",i); - 浮点数(float)
str.Format("%f",i); - 字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值
str = username; - 对于Format所不支持的数据类型,可以通过上面所说的关于其它数据类型转化到char *的方法先转到char *,然后赋值给CString变量。
四、BSTR、_bstr_t与CComBSTR
- CComBSTR 是ATL对BSTR的封装,_bstr_t是C++对BSTR的封装,BSTR是32位指针,但并不直接指向字串的缓冲区。
char *转换到BSTR可以这样: BSTR b=_com_util::ConvertStringToBSTR("数据");///使用前需要加上comutil.h和comsupp.lib SysFreeString(bstrValue); 反之可以使用 char *p=_com_util::ConvertBSTRToString(b); delete p; 具体可以参考一,二段落里的具体说明。
CComBSTR与_bstr_t对大量的操作符进行了重载,可以直接进行=,!=,==等操作,所以使用非常方便。 特别是_bstr_t,建议大家使用它。
五、VARIANT 、_variant_t 与 COleVariant
Hello, World! (System::String)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string) 转自-------------------------------http://hi.baidu.com/%D0%BB%B4%BA%D2%B5/blog/item/470df90259efec094afb5127.html
|