C中字符串以及数据类型的转换方法汇总(帖子汇编).doc

C中字符串以及数据类型的转换方法汇总(帖子汇编).doc

  1. 1、本文档共43页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
C++中数据类型的转换 在C++中有很多的数据类型,在编程的时候,对数据类型进行转换很常遇到的, 1、如何将CString变量转换为int变量 2、如何将int变量转换CString为变量 CString?ss=1212.12;? int?temp=atoi(ss);? CString?aa;? aa.Format(%d,temp);? 3、CString转换为Double 4、Double转换为CString CString?strValue(1.234);? double?dblValue;?? dblValue?=?atof((LPCTSTR)strValue);? 5、char*转换成CString char chArray[] = This is a test; char * p = This is a test;   或 LPSTR p = This is a test;   或在已定义Unicode应的用程序中 TCHAR * p = _T(This is a test);   或 LPTSTR p = _T(This is a test); CString theString = chArray; theString.Format(_T(%s), chArray); theString = p; 6、CString转换成char*   若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法: 方法一,使用强制转换。例如: CString?theString(?This?is?a?test?); LPTSTR?lpsz?=(LPTSTR)(LPCTSTR)theString;?? 方法二,使用strcpy。例如: CString?theString(?This?is?a?test?); LPTSTR?lpsz?=?new?TCHAR[theString.GetLength()+1]; _tcscpy(lpsz,?theString);?   需要说明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二个参数是?const?wchar_t*?(Unicode)或const?char*?(ANSI),系统编译器将会自动对其进行转换。 方法三,使用CString::GetBuffer。例如: CString?s(_T(This?is?a?test?)); LPTSTR?p?=?s.GetBuffer(); //?在这里添加使用p的代码 if(p?!=?NULL)?*p?=?_T(\0); s.ReleaseBuffer();? //?使用完后及时释放,以便能使用其它的CString成员函数? 7、BSTR转换成char* 方法一,使用ConvertBSTRToString。例如: #include? #pragma?comment(lib,?comsupp.lib) int?_tmain(int?argc,?_TCHAR*?argv[]){ BSTR?bstrText?=?::SysAllocString(LTest); char*?lpszText2?=?_com_util::ConvertBSTRToString(bstrText); SysFreeString(bstrText);?//?用完释放 delete[]?lpszText2; return?0; }?? 方法二,使用_bstr_t的赋值运算符重载。例如: _bstr_t?b?=?bstrText; char*?lpszText2?=?b;? 8、char*转换成BSTR   方法一,使用SysAllocString等API函数。例如: BSTR?bstrText?=?::SysAllocString(LTest); BSTR?bstrText?=?::SysAllocStringLen(LTest,4); BSTR?bstrText?=?::SysAllocStringByteLen(Test,4);?   方法二,使用COleVariant或_variant_t。例如: //COleVariant?strVar(This?is?a?test); _variant_t?strVar(This?is?a?test); BSTR?bstrText?=?strVar.bstrVal;?   方法三,使用_bstr_t,这是一种最简单的方法。例如: BSTR?bstrText?=?_bstr_t(This?is?a?test);?   方法四,使用CComBSTR。例如: BSTR?bstrText?=?CComBSTR(This?is?a?tes

文档评论(0)

shaoye348 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档