轉: http://blog.csdn.net/flydream0/article/details/8543525/
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 *
如下:
- CString str1 ="123";
- char *p =(LPSTR)(LPCSTR)str1;
但好像官方並不建議這么做,而建議采用下面這種方式:
- CString str1 ="123";
- char *t1 =str1.GetBuffer(str1.GetLength());
- str1.ReleaseBuffer();
- //do something with t1
網上也有人說是這樣t1 =str1.GetBuffer(0);但其實我在實測時並沒發現str1.GetBuffer(str1.GetLenth())與str.GetBuffer(0)返回值有啥區別,MSDN中相應說明如下:
- CString::GetBuffer
- LPTSTR GetBuffer( int nMinBufLength );
- throw( CMemoryException );
- Return Value
- An LPTSTR pointer to the object’s (null-terminated) character buffer.
- Parameters
- nMinBufLength
- The minimum size of the character buffer in characters. This value does not include space for a null terminator.
- Remarks
- 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.
- If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
- 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.
- The buffer memory will be freed automatically when the CString object is destroyed.
- 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
- char *str ="aaaa"
- CString str1(str);
- //...
2.3 CString -->int
在寬字符字符模式下,這個非常簡單:
- CString str1 ="123";
- int i =atoi(str1);
- //do something with i
2.4 int -->CString
- int i =100;
- CString str;
- str.Format("%d",i);
- //...
3 Unicode編碼模式
3.1 CString -->char *
在這種情況下,上述所說的轉化全是浮雲,目前只發現可以用WideCharToMultiByte函數來實現.
如下 :
- CString str1 =_T("123");
- int len =WideCharToMultiByte(CP_ACP,0,str1,-1,NULL,0,NULL,NULL);
- char *ptxtTemp =new char[len +1];
- WideCharToMultiByte(CP_ACP,0,str1,-1,ptxtTemp,len,NULL,NULL );
- //...
- delete[] ptxtTemp;
3.2 char * -->CString
還是可以如下:
- char *p ="test";
- CString str(p);
- //...
3.3 CString -->int
在這種情況下atoi不再適用,其實可以用swscanf,如下:
- CString str2 =_T("100");
- int i;
- swscanf(str2,_T("%d"),&i);
3.4 int -->CString
這個其實最簡單了,如下:
- int j =100;
- CString str3;
- str3.Format(_T("%d"),j);
4 結束
另外,有關ANSI與Unicode之間的轉換及UTF-8與Unicode之間的轉換可以參與下面這個鏈接: