CString与char *互转总结

1 前言

今天在网上看论坛,发现大家对CString与Char *互转各说一词,其实我发现提问者所说的情况与回答问题的人完全不是同一情况,这里做一总结.


首先大家得清楚一件事,一般在网上提出问题的人大部分使用的都是VC,那么你就应该知道,在VC下编程,工程属性中有一属性Charecter Set属性,其值可以设置为Use Multi-Byte Charecter Set 和 Use Unicode Charecter Set 这两种选择,具默认情况下工程是采用了Use Unicode Charecter Set选项.如我使用的VS2010的工程属性中如下:


VC在处理CString类型字符时,在这两种不种选择的处理结果也是完全不一样的,而网上那么答复大都是针对假设提问者是使用了Use Mult-Byte Chracter Set的前提下,但大多提这个问题的人都是使用了后者的情况的人.


暂且将Use Mult-Byte Chracter Set称之为宽字节字符模式,而Use Unicode Charecter Set称之为Unicode编码模式.


2 宽字节字符模式

首先讨论一下宽字符字符模式下的CStirng与Char *之间的互转,在这种情况下互换很简单:

2.1 CString -->char *

如下:

  1. CString str1 ="123";  
  2. char *p =(LPSTR)(LPCSTR)str1;  

但好像官方并不建议这么做,而建议采用下面这种方式:

  1. CString str1 ="123";  
  2. char *t1 =str1.GetBuffer(str1.GetLength());  
  3. str1.ReleaseBuffer();  
  4. //do something with t1  

网上也有人说是这样t1 =str1.GetBuffer(0);但其实我在实测时并没发现str1.GetBuffer(str1.GetLenth())与str.GetBuffer(0)返回值有啥区别,MSDN中相应说明如下:

[plain] view plaincopy
  1. CString::GetBuffer   
  2.   
  3. LPTSTR GetBuffer( int nMinBufLength );  
  4.  throw( CMemoryException );  
  5.    
  6. Return Value  
  7.    
  8. An LPTSTR pointer to the object’s (null-terminated) character buffer.  
  9.    
  10. Parameters  
  11.    
  12. nMinBufLength  
  13.    
  14. The minimum size of the character buffer in characters. This value does not include space for a null terminator.  
  15.    
  16. Remarks  
  17.    
  18. Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.  
  19.    
  20. If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.   
  21.   
  22. The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.  
  23.    
  24. The buffer memory will be freed automatically when the CString object is destroyed.   
  25.   
  26. Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length.   

由上可知,GetBuffer的参数nMinBufLength为最小缓冲区长度,但实际结果没啥区别...

2.2 char * -->CString

  1. char *str ="aaaa"  
  2.   
  3. CString str1(str);  
  4. //...  

2.3 CString -->int

在宽字符字符模式下,这个非常简单:

  1. CString str1 ="123";  
  2. int i =atoi(str1);  
  3. //do something with i  

2.4 int -->CString

  1. int i =100;  
  2. CString str;  
  3. str.Format("%d",i);  
  4. //...  

3 Unicode编码模式

3.1 CString -->char *

在这种情况下,上述所说的转化全是浮云,目前只发现可以用WideCharToMultiByte函数来实现.

如下 :

  1. CString str1 =_T("123");  
  2. int len =WideCharToMultiByte(CP_ACP,0,str1,-1,NULL,0,NULL,NULL);  
  3. char *ptxtTemp =new char[len +1];  
  4. WideCharToMultiByte(CP_ACP,0,str1,-1,ptxtTemp,len,NULL,NULL );  
  5.   
  6. //...  
  7. delete[] ptxtTemp;  

3.2 char * -->CString

还是可以如下:

  1. char *p ="test";  
  2. CString str(p);  
  3. //...  

3.3 CString -->int

在这种情况下atoi不再适用,其实可以用swscanf,如下:

  1. CString str2 =_T("100");  
  2. int i;  
  3. swscanf(str2,_T("%d"),&i);  

3.4 int -->CString

这个其实最简单了,如下:

  1. int j =100;  
  2. CString str3;  
  3. str3.Format(_T("%d"),j);  

4 结束

另外,有关ANSI与Unicode之间的转换UTF-8与Unicode之间的转换可以参与下面这个链接:

http://www.cnblogs.com/gakusei/articles/1585211.html

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随笔 - 764 文章 - 3 评论 - 196 CString,string,char*之间的换() 这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差。string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的;char*是从学习C语言开始就已经和我们形影不离的了,有许多API都是以char*作为参数输入的。所以熟练掌握三者之间的换十分必要。 以下我用简单的图示指出三者之间的关系,并以标号对应换的方法。 1 string to CString CString.format("%s",string.c_str()); 2 CString to string string str(CString.GetBuffer(str.GetLength())); 3 string to char * char *p=string.c_str(); 4 char * to string string str(char*); 5 CString to char * strcpy(char,CString,sizeof(char)); 6 char * to CString CString.format("%s",char*); CString的format方法是非常好用的。string的c_str()也是非常常用的,但要注意和char *换时,要把char定义成为const char*,这样是最安全的。 以上函数UNICODE编码也没问题:unicode下照用,加个_T()宏就行了,像这样子_T("%s") 补充: CString 可能是 CStringW/CStringA,在与 string 换时,如果是 CStringW,还涉及编码换问题。下面以 CStringA 来说明。 1 string to CString CString.format("%s",string.c_str()); CStringA = string.c_str() 就可以了 2 CString to string string str(CString.GetBuffer(str.GetLength())); GetBuffer 有参数的话,可能导致内部的分配空间动作,要进行后续 ReleaseBuffer 操作。 string = CStringA string = CStringA.GetBuffer(); 3 string to char * char *p=string.c_str(); 4 char * to string string str(char*); 5 CString to char * strcpy(char *,CString,sizeof(char)); 按照 3 风格,这里应该 char * = CStringA; 或者 char *p = CStringA.GetBuffer(); 6 char * to CString CStringA = char * 就可以了

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值