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

首頁 > 學院 > 開發設計 > 正文

CString與int,char *互轉總結

2019-11-11 06:36:08
字體:
來源:轉載
供稿:網友

轉自: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之間的轉換可以參與下面這個鏈接:

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


上一篇:面向對象

下一篇:[Leetcode] 16. 3Sum Closest

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岑溪市| 仁寿县| 井冈山市| 汶上县| 邓州市| 凤城市| 西乌| 芜湖县| 双江| 延安市| 安西县| 霍州市| 右玉县| 和平区| 修文县| 开平市| 海伦市| 黄龙县| 阿荣旗| 左云县| 泽库县| 宁波市| 澄迈县| 台东县| 乌拉特前旗| 扬中市| 阿拉善盟| 荆州市| 屏山县| 武威市| 乌恰县| 安泽县| 武胜县| 鸡东县| 温泉县| 蛟河市| 浙江省| 云霄县| 渑池县| 云霄县| 辽宁省|