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

首頁 > 編程 > C > 正文

CString,字符串,整數等相互轉換方法(推薦)

2020-01-26 14:25:27
字體:
來源:轉載
供稿:網友

字符串轉int

int atoi(const char *string);
_int64 _atoi64(const char *string);
int _wtoi(const wchar_t *string);
_int64 _ wtoi64(const char *string);

字符串轉long

long atol(const char * string);
long _wtol(const wchar_t *string);

字符串轉double

double atof(const char *string);
double _wtof(const wchar_t *string);

int轉字符串

cahr *_itoa( int value,char *string,int radix);
char *_i64toa(_int64 value,char *string,int radix);
char * _ui64toa(unsigned _int64 value,char *string, int radix);
wchar_t * _itow(int value,wchar_t *string, int radix);
wchar_t * _i64tow(_int64 value,wchar_t *string, int radix);
wchar_t * _ui64tow(unsigned _int64 value,wchar_t *string, int radix);

參數的意義:value 是指要轉換的整數,sring 是用來存放轉換后結果的便利,radix是用來說明轉換成幾進制的數據,默認值是十進制數的。轉換的進制范圍是二進制到三十六進制。

long轉字符串

char *_ltoa( long value,char *string, int radix );
wchar_t *_ltow( long value, wchar_t *string, int radix );

其中,參數 value 為被轉換的值,參數string為字符串緩沖區,radix為進制。

double轉字符串

char *_fcvt( double value, int count, int *dec, int *sign );

其中參數value 為雙精度數,參數count為轉換的小數點后面的位數,dec表示小數點的位置, sign 表示符號。

(1) char*轉換成CString

若將char*轉換成CString,除了直接賦值外,還可使用CString::Format進行。例如:

char chArray[] = "Char test";TCHAR * p = _T("Char test");( 或LPTSTR p = _T("Char test");)CString theString = chArray;theString.Format(_T("%s"), chArray);theString = p;

(2) CString轉換成char*

若將CString類轉換成char*(LPSTR)類型,常常使用下列三種方法:

方法一,使用強制轉換。例如:

CString theString( (_T("Char test "));LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

方法二,使用strcpy。例如:

CString theString( (_T("Char test "));LPTSTR lpsz = new TCHAR[theString.GetLength()+1];_tcscpy(lpsz, theString);

需要說明的是,strcpy(或可移值的_tcscpy)的第二個參數是 const wchar_t* (Unicode)或const char* (ANSI),系統編譯器將會自動對其進行轉換。

方法三,使用CString::GetBuffer。

如果你需要修改 CString 中的內容,它有一個特殊的方法可以使用,那就是 GetBuffer,它的作用是返回一個可寫的緩沖指針。 如果你只是打算修改字符或者截短字符串,例如:

CString s(_T("Char test "));LPTSTR p = s.GetBuffer(); LPTSTR dot = strchr(p, ''.'');// 在這里添加使用p的代碼if(p != NULL) *p = _T('');s.ReleaseBuffer();// 使用完后及時釋放,以便能使用其它的CString成員函數

在 GetBuffer 和 ReleaseBuffer 之間這個范圍,一定不能使用你要操作的這個緩沖的 CString 對象的任何方法。因為 ReleaseBuffer 被調用之前,該 CString 對象的完整性得不到保障。

以上就是小編為大家帶來的CString,字符串,整數等相互轉換方法(推薦)的全部內容了,希望對大家有所幫助,多多支持武林網~

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 沁水县| 赤城县| 天镇县| 西贡区| 青浦区| 桦川县| 兴文县| 沈阳市| 连南| 东乌珠穆沁旗| 登封市| 陕西省| 昌都县| 兴城市| 依兰县| 海盐县| 高邑县| 叙永县| 微山县| 体育| 河南省| 玛曲县| 舟山市| 崇仁县| 井陉县| 沈丘县| 施秉县| 裕民县| 台东市| 故城县| 阿巴嘎旗| 建始县| 镇平县| 福建省| 阜南县| 手机| 磐石市| 霞浦县| 宜兴市| 卢氏县| 沈阳市|