cstringintstringcharbstr之间的转换

上传人:xiao****1972 文档编号:84137937 上传时间:2019-03-02 格式:DOC 页数:14 大小:110.50KB
返回 下载 相关 举报
cstringintstringcharbstr之间的转换_第1页
第1页 / 共14页
cstringintstringcharbstr之间的转换_第2页
第2页 / 共14页
cstringintstringcharbstr之间的转换_第3页
第3页 / 共14页
cstringintstringcharbstr之间的转换_第4页
第4页 / 共14页
cstringintstringcharbstr之间的转换_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《cstringintstringcharbstr之间的转换》由会员分享,可在线阅读,更多相关《cstringintstringcharbstr之间的转换(14页珍藏版)》请在金锄头文库上搜索。

1、一CString, int, string, char*之间的转换string 转 CStringCString.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();1.string - CString CString. Format(%s, string.c_str(); 用c_

2、str()确实比data()要好.2.char - string string s(char *); 你的只能初始化,在不是初始化的地方最好还是用assign().3.CString - string string s(CString.GetBuffer(); GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.C+标准函数库中:有三个函数可以将字符串的内容转换为字符数组和Cstring1.data(),返回没有”0“的字符串数组2.c_str(),返回有”0“的字符串数组3.copy()CString 和 int互转将字符转换为整数,可以使用atoi

3、、_atoi64或atol.将数字转换为CString变量,可以使用CString的Format函数。如(1)CString s;int i = 64;s.Format(%d, i)Format函数的功能很强,值得你研究一下。(2)void CStrDlg:OnButton1()/ TODO: Add your control notification handler code hereCString ss=1212.12;int temp=atoi(ss); /字符转换为整数CString aa;aa.Format(%d,temp); /整数转换为字符AfxMessageBox(var is

4、 + aa);CString和char*互转(1)char * - CStringCString strtest;char * charpoint;charpoint=give string a value;strtest=charpoint;(2)CString - char *charpoint=strtest.GetBuffer(strtest.GetLength();标准C里没有string,char *=char =string可以用CString.Format(%s,char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR

5、)CString就可以了。CString- char100char a100;CString str(aaaaaa);strncpy(a,(LPCTSTR)str,sizeof(a);CString类型的转换成int(1)CString类型的转换成int,可以使用atoi、_atoi64或atol。例:CString aaa = 16 ; int int_chage = atoi(lpcstr)aaa) ;(2)将数字转换为CString变量,可以使用CString的Format函数。例:CString s;int i = 64;s.Format(%d, i)CString ss=1212.1

6、2;int temp=atoi(ss);CString aa;aa.Format(%d,temp);如果是使用char数组,也可以使用sprintf函数。数字-字符串除了用CString:Format,还有FormatV、sprintf和不需要借助于Afx的itoastring-char* string aa(aaa);char *c=aa.c_str();注:1.string.c_str()只能转换成const char *:const char *c=aa.c_str();2.cannot convert from const char * to char *3.要转成char *这样写:

7、 string mngName; char t200; memset(t,0,200); strcpy(t,mngName.c_str();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); SysFreeStri

8、ng(bstrText); / 用完释放 delete lpszText2; return 0; 方法二:使用_bstr_t的赋值运算符重载。例如:_bstr_t b = bstrText;char* lpszText2 = b;char*转换成BSTR方法一:使用SysAllocString等API函数。例如: BSTR bstrText = :SysAllocString(LTest); BSTR bstrText = :SysAllocStringLen(LTest,4); BSTR bstrText = :SysAllocStringByteLen(Test,4); 方法二:使用COl

9、eVariant或_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 test); 或CComBSTR bstr(This is a test);BSTR bstrText =

10、bstr.m_str; 方法五,使用ConvertStringToBSTR。例如:char* lpszText = Test;BSTR bstrText = _com_util:ConvertStringToBSTR(lpszText);CString转换成BSTR通常是通过使用CStringT:AllocSysString来实现。例如:CString str(This is a test);BSTR bstrText = str.AllocSysString();SysFreeString(bstrText); / 用完释放BSTR转换成CString一般可按下列方法进行:BSTR bstr

11、Text = :SysAllocString(LTest);CStringA str;str.Empty();str = bstrText; 或CStringA str(bstrText);ANSI、Unicode和宽字符之间的转换方法一:使用MultiByteToWideChar将ANSI字符转换成Unicode字符,使用WideCharToMultiByte将Unicode字符转换成ANSI字符。方法二:使用“_T”将ANSI转换成“一般”类型字符串,使用“L”将ANSI转换成Unicode在托管C+环境中还可使用S将ANSI字符串转换成String*对象。例如:TCHAR tstr =

12、_T(this is a test);wchar_t wszStr = LThis is a test;String* str = S”This is a test”; 方法三:使用ATL 7.0的转换宏和类。ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类:其中,第一个C表示“类”,以便于ATL 3.0宏相区别,第二个C表示常量,2表示“to”,EX表示要开辟一定大小的缓冲。SourceType和DestinationType可以是A、 T、W和OLE,其含义分别是ANSI、Unicode、“一般”类型和OLE字符串。例如:CA2CT就是将ANSI转换成一般类型的字

13、符串常量。下面是一些示例代码:LPTSTR tstr= CA2TEX(this is a test);LPCTSTR tcstr= CA2CT(this is a test);wchar_t wszStr = LThis is a test;char* chstr = CW2A(wszStr);二VC字符串转换(BSTR CString)一BSTR、LPSTR和LPWSTR在Visual C+.NET的所有编程方式中,我们常常要用到这样的一些基本字符串类型,如BSTR、LPSTR和LPWSTR等。之所以出现类似上述的这些数据类型,是因为不同编程语言之间的数据交换以及对ANSI、Unicode和

14、多字节字符集(MBCS)的支持。那么什么是BSTR、LPSTR以及LPWSTR呢?1BSTR(Basic STRing,Basic字符串)是一个OLECHAR*类型的Unicode字符串。它被描述成一个与自动化相兼容的类型。由于操作系统提供相应的API函数(如SysAllocString)来管理它以及一些默认的调度代码,因此BSTR实际上就是一个COM字符串,但它却在自动化技术以外的多种场合下得到广泛使用。2LPSTR和LPWSTR是Win32和VC+所使用的一种字符串数据类型。LPSTR被定义成是一个指向以NULL(0)结尾的8位ANSI字符数组指针,而LPWSTR是一个指向以NULL结尾的16位双字节字符数组指针。在VC+中,还有类似的字符串类型,如LPTSTR、LPCTSTR等。例如,LPCTSTR是指“long pointer to a constant generic string”,表示“一个指向一般字符串常量的长指针类型”,与C/C+的const char*相映射,而LPTSTR映射为 char*。一般地,还有下列类型定义:#ifdef UNICODE typedef LPWSTR LPTSTR; typedef LPCWSTR LPCTSTR;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号