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

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

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

2019-11-11 07:06:13
字體:
供稿:網(wǎng)友

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

1 前言

今天在網(wǎng)上看論壇,發(fā)現(xiàn)大家對CString與Char *互轉(zhuǎn)各說一詞,其實(shí)我發(fā)現(xiàn)提問者所說的情況與回答問題的人完全不是同一情況,這里做一總結(jié).

首先大家得清楚一件事,一般在網(wǎng)上提出問題的人大部分使用的都是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類型字符時,在這兩種不種選擇的處理結(jié)果也是完全不一樣的,而網(wǎng)上那么答復(fù)大都是針對假設(shè)提問者是使用了Use Mult-Byte Chracter Set的前提下,但大多提這個問題的人都是使用了后者的情況的人.

暫且將Use Mult-Byte Chracter Set稱之為寬字節(jié)字符模式,而Use Unicode Charecter Set稱之為Unicode編碼模式.

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

首先討論一下寬字符字符模式下的CStirng與Char *之間的互轉(zhuǎ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)上也有人說是這樣t1 =str1.GetBuffer(0);但其實(shí)我在實(shí)測時并沒發(fā)現(xiàn)str1.GetBuffer(str1.GetLenth())與str.GetBuffer(0)返回值有啥區(qū)別,MSDN中相應(yīng)說明如下: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ū)長度,但實(shí)際結(jié)果沒啥區(qū)別...

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 *

在這種情況下,上述所說的轉(zhuǎn)化全是浮云,目前只發(fā)現(xiàn)可以用WideCharToMultiByte函數(shù)來實(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

這個其實(shí)最簡單了,如下:

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

4 結(jié)束

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

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 肃宁县| 抚州市| 舒兰市| 确山县| 驻马店市| 准格尔旗| 将乐县| 广平县| 云安县| 榆中县| 个旧市| 德保县| 泰和县| 曲阜市| 莱阳市| 礼泉县| 收藏| 丹棱县| 开平市| 渝北区| 田阳县| 兴业县| 枞阳县| 南溪县| 汕尾市| 板桥市| 平陆县| 新邵县| 九寨沟县| 恩施市| 白沙| 修武县| 乳山市| 尼玛县| 信阳市| 天津市| 扶风县| 林州市| 寻乌县| 凉城县| 休宁县|