VC下CString类型与int 、float等数据类型的相互转换
2014-03-13 20:54
597 查看
一、常用转换
1. CString --> int转换
CString str("1234");
int i= _ttoi(str);
2. CString --> float转换
方法一:
CString str;
float fi;
fi=_tstof(str); //转成了double
方法二:
float i = (float)atof(str.GetBuffer(str.GetLength()));
方法三:
float f = atof((LPCSTR)str);
3. int --> CString 转换
wScale = 300 * _tstof(dlg.GetRate());
str.Format(_T("%d"), wScale);
4. float --> CString 转换
float m_Result = 99.9;
CString m_ShowData.Format("%f", m_Result);
二、将CString转换为double(或float)的3种方法
CString strFloat;
float flt;
//method1:
flt = (float)atof((char *)(LPTSTR)(LPCTSTR)mstrFloat);
//method2:
flt = (float)atof((char *)m_eps.GetBuffer(strFloat.GetLength()));
strFloat.ReleaseBuffer();
//method3:
//Convert CString to double
static BOOL _AtlSimpleFloatParse(LPCTSTR lpszText, double& d)
{
ATLASSERT(lpszText != NULL);
while (*lpszText == ' '|| *lpszText == '/t')
{
lpszText++;
}
TCHAR chFirst = lpszText[0];
d = _tcstod(lpszText,(LPTSTR*)&lpszText);
if (d == 0.0 && chFirst != '0')
{
return FALSE; //could not convert
}
while (*lpszText == ' '|| *lpszText == '/t')
{
lpszText++;
}
if (*lpszText != '/0')
{
return FALSE; //not terminated properly
}
return TRUE;
}
不过前面两种方法在VS2005下运行结果不正确,在VC6.0开发环境下是可以的。
1. CString --> int转换
CString str("1234");
int i= _ttoi(str);
2. CString --> float转换
方法一:
CString str;
float fi;
fi=_tstof(str); //转成了double
方法二:
float i = (float)atof(str.GetBuffer(str.GetLength()));
方法三:
float f = atof((LPCSTR)str);
3. int --> CString 转换
wScale = 300 * _tstof(dlg.GetRate());
str.Format(_T("%d"), wScale);
4. float --> CString 转换
float m_Result = 99.9;
CString m_ShowData.Format("%f", m_Result);
二、将CString转换为double(或float)的3种方法
CString strFloat;
float flt;
//method1:
flt = (float)atof((char *)(LPTSTR)(LPCTSTR)mstrFloat);
//method2:
flt = (float)atof((char *)m_eps.GetBuffer(strFloat.GetLength()));
strFloat.ReleaseBuffer();
//method3:
//Convert CString to double
static BOOL _AtlSimpleFloatParse(LPCTSTR lpszText, double& d)
{
ATLASSERT(lpszText != NULL);
while (*lpszText == ' '|| *lpszText == '/t')
{
lpszText++;
}
TCHAR chFirst = lpszText[0];
d = _tcstod(lpszText,(LPTSTR*)&lpszText);
if (d == 0.0 && chFirst != '0')
{
return FALSE; //could not convert
}
while (*lpszText == ' '|| *lpszText == '/t')
{
lpszText++;
}
if (*lpszText != '/0')
{
return FALSE; //not terminated properly
}
return TRUE;
}
不过前面两种方法在VS2005下运行结果不正确,在VC6.0开发环境下是可以的。
相关文章推荐
- VC 中 Cstring 与 int、float、double、char、string数据类型的相互转换
- VC 中 Cstring 与 int、float、double、char、string数据类型的相互转换
- MFC----Cstring 与 int、float、double 数据类型的相互转换
- vc++ Format 用法总结~~CString类型与其它数据类型相互转换
- VC++中几种数据类型(CString, char*, char, ******等)之间的相互转换
- vc++中int,char*, float,double,CString之间的类型转换
- Cstring与char、string、int等数据类型的相互转换方法
- Java数据类型中String、Integer、int的相互转换以及日期的转换
- C++中string,char,int,double等数据类型的相互转换及与ASCII码的转换
- cstring和相关类型的相互转换int,double,char*
- int、double、boolean、char、float、long、Object等七种数据类型转换成String数据类型 用到的方法是String.valueOf();
- VC++中,CString,in,char,等数据类型的相互转化
- c/c++数据类型转换1(float,char,string,CString )
- 摘要:关于VC中的数据类型转换BSTR、char*和CString的深入研究。
- VC中的数据类型转换BSTR、char*和CString
- CString TCHAR BSTR BYTE WORD DWORD COLORREF VARIANT等数据类型相互转换
- Java 数据类型转换,String->float,float->int ,String<->int (2013-07-24 14:01:57)
- c++ CString string char* char int类型的相互转换
- 类型转换总结(CString 、char*、int、float、_bstr_t、LPCWSTR、LPSTR)
- VC++中,CString,in,char,等数据类型的相互转化