轉(zhuǎn)自:http://blog.csdn.net/flydream0/article/details/8543525
今天在網(wǎng)上看論壇,發(fā)現(xiàn)大家對(duì)CString與Char *互轉(zhuǎn)各說(shuō)一詞,其實(shí)我發(fā)現(xiàn)提問(wèn)者所說(shuō)的情況與回答問(wèn)題的人完全不是同一情況,這里做一總結(jié).
首先大家得清楚一件事,一般在網(wǎng)上提出問(wèn)題的人大部分使用的都是VC,那么你就應(yīng)該知道,在VC下編程,工程屬性中有一屬性Charecter Set屬性,其值可以設(shè)置為Use Multi-Byte Charecter Set 和 Use Unicode Charecter Set 這兩種選擇,具默認(rèn)情況下工程是采用了Use Unicode Charecter Set選項(xiàng).如我使用的VS2010的工程屬性中如下:

VC在處理CString類型字符時(shí),在這兩種不種選擇的處理結(jié)果也是完全不一樣的,而網(wǎng)上那么答復(fù)大都是針對(duì)假設(shè)提問(wèn)者是使用了Use Mult-Byte Chracter Set的前提下,但大多提這個(gè)問(wèn)題的人都是使用了后者的情況的人.
暫且將Use Mult-Byte Chracter Set稱之為寬字節(jié)字符模式,而Use Unicode Charecter Set稱之為Unicode編碼模式.
首先討論一下寬字符字符模式下的CStirng與Char *之間的互轉(zhuǎn),在這種情況下互換很簡(jiǎn)單:
如下:
CString str1 ="123"; char *p =(LPSTR)(LPCSTR)str1; 但好像官方并不建議這么做,而建議采用下面這種方式:CString str1 ="123"; char *t1 =str1.GetBuffer(str1.GetLength()); str1.ReleaseBuffer(); //do something with t1 網(wǎng)上也有人說(shuō)是這樣t1 =str1.GetBuffer(0);但其實(shí)我在實(shí)測(cè)時(shí)并沒(méi)發(fā)現(xiàn)str1.GetBuffer(str1.GetLenth())與str.GetBuffer(0)返回值有啥區(qū)別,MSDN中相應(yīng)說(shuō)明如下: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的參數(shù)nMinBufLength為最小緩沖區(qū)長(zhǎng)度,但實(shí)際結(jié)果沒(méi)啥區(qū)別...在寬字符字符模式下,這個(gè)非常簡(jiǎn)單:
CString str1 ="123"; int i =atoi(str1); //do something with i在這種情況下,上述所說(shuō)的轉(zhuǎn)化全是浮云,目前只發(fā)現(xiàn)可以用WideCharToMultiByte函數(shù)來(lái)實(shí)現(xiàn).
如下 :
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;還是可以如下:
char *p ="test"; CString str(p); //...在這種情況下atoi不再適用,其實(shí)可以用swscanf,如下:
CString str2 =_T("100"); int i; swscanf(str2,_T("%d"),&i);這個(gè)其實(shí)最簡(jiǎn)單了,如下:
int j =100; CString str3; str3.Format(_T("%d"),j);另外,有關(guān)ANSI與Unicode之間的轉(zhuǎn)換及UTF-8與Unicode之間的轉(zhuǎn)換可以參與下面這個(gè)鏈接:
http://www.cnblogs.com/gakusei/articles/1585211.html
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注