MFC中CString類字串與長整型、浮點型、字元陣列char資料之間的相互轉換
阿新 • • 發佈:2018-12-13
一、長整型資料與CString類字串相互轉換
1.將長整型資料轉換為CString字串類
CString str;
long ld;
str.Format(_T("%ld"),ld);
2.將CString字串類轉換為長整型資料
CString str;
long ld;
ld=_wtol(str);
_wtol()在<stdlib.h> and <wchar.h>中
二、浮點型資料與CString類字串相互轉換
1.將浮點型資料轉換為CString類字串
CString str;
double ss;
str.Format(_T("%f"),ss);
2.將CString類字串轉換為浮點型資料
CString str;
double ss;
ss=_wtof(str);
_wtof()在<stdlib.h> or<wchar.h>中
三、將整型資料轉換為CString字串類
CString str;
int n=1;
str.Format(_T("%c"),n+65);//這樣str=”B”;
四、字元陣列與CString字串類相互轉換
1. 將字元陣列轉換為CString字串類
CString str;
char buffer[100];
str.Format(_T("%s"),buffer);
或者str=buffer;
2. 將CString字串類轉換為字元陣列
CString str;
char buffer[100];
int i;
for(i=0;i<str.GetLength();i++)
{
buffer[i]=(char)(str.GetAt(i));
}
buffer[i]='\0';