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 *

如下:

[cpp] view plain copy
  1. CString str1 ="123";  
  2. char *p =(LPSTR)(LPCSTR)str1;  

但好像官方並不建議這麼做,而建議採用下面這種方式:

[cpp] view plain copy
  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 plain copy
  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

[cpp] view plain copy
  1. char *str ="aaaa"  
  2.   
  3. CString str1(str);  
  4. //...  

2.3 CString -->int

在寬字符字符模式下,這個非常簡單:

[cpp] view plain copy
  1. CString str1 ="123";  
  2. int i =atoi(str1);  
  3. //do something with i  

2.4 int -->CString

[cpp] view plain copy
  1. int i =100;  
  2. CString str;  
  3. str.Format("%d",i);  
  4. //...  

3 Unicode編碼模式

3.1 CString -->char *

在這種情況下,上述所說的轉化全是浮雲,目前只發現可以用WideCharToMultiByte函數來實現.

如下 :

[cpp] view plain copy
  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

還是可以如下:

[cpp] view plain copy
  1. char *p ="test";  
  2. CString str(p);  
  3. //...  

3.3 CString -->int

在這種情況下atoi不再適用,其實可以用swscanf,如下:

[cpp] view plain copy
  1. CString str2 =_T("100");  
  2. int i;  
  3. swscanf(str2,_T("%d"),&i);  

3.4 int -->CString

這個其實最簡單了,如下:

[cpp] view plain copy
  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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章