国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

CString與int,char *互轉(zhuǎn)總結(jié)

2019-11-11 06:32:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

轉(zhuǎn)自:http://blog.csdn.net/flydream0/article/details/8543525

1 前言

今天在網(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編碼模式.

2 寬字節(jié)字符模式

首先討論一下寬字符字符模式下的CStirng與Char *之間的互轉(zhuǎn),在這種情況下互換很簡(jiǎn)單:

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  網(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ū)別...

2.2 char * -->CString

char *str ="aaaa"    CString str1(str);  //...  

2.3 CString -->int

在寬字符字符模式下,這個(gè)非常簡(jiǎn)單:

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 *

在這種情況下,上述所說(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;  

3.2 char * -->CString

還是可以如下:

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

3.3 CString -->int

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

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

3.4 int -->CString

這個(gè)其實(shí)最簡(jiǎn)單了,如下:

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

4 結(jié)束

另外,有關(guān)ANSI與Unicode之間的轉(zhuǎn)換UTF-8與Unicode之間的轉(zhuǎn)換可以參與下面這個(gè)鏈接:

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 溧阳市| 阜宁县| 清镇市| 达日县| 寻乌县| 白山市| 宜都市| 云林县| 会理县| 翁牛特旗| 鹿邑县| 新宁县| 盐边县| 叙永县| 徐闻县| 高雄县| 青州市| 逊克县| 彩票| 新闻| 札达县| 杭锦后旗| 定日县| 石楼县| 阳曲县| 庐江县| 自治县| 长汀县| 蒙山县| 红桥区| 西乌珠穆沁旗| 寿宁县| 巴彦淖尔市| 昌吉市| 乌鲁木齐县| 宾阳县| 琼中| 吉木萨尔县| 长宁县| 沧源| 伽师县|