如何进行CString变量和int变量的转换
编号: QA003587
建立日期: 2000年11月26日 最后修改日期: 2003年12月28日
所属类别: C/C++ - 其他方面
操作系统: win98
编程工具: VC++
问题: 请问如何将CString变量转换为int变量,如何将int变量转换CString为变量?
水平: 刚入门(cj)
将字符转换为整数,可以使用atoi、_atoi64或atol。
而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
s.Format("%d", i)
Format函数的功能很强,值得你研究一下。
如果是使用char数组,也可以使用sprintf函数。
wzy的意见:
void CStrDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString ss="1212.12";
int temp=atoi(ss);
CString aa;
aa.Format("%d",temp);
AfxMessageBox("var is " + aa);
}
心水的意见:
数字->字符串除了用CString::Format,还有FormatV、sprintf和不需要借助于Afx的itoa。查MSDN有很详细的说明。
如何实现double和CString两种数据类型的转换
编号: QA003773
建立日期: 2001年1月16日 最后修改日期: 2004年9月5日
所属类别: C/C++ - 其他方面
操作系统:Windows98
编程工具:Visual C++6.0
问题:请问在VC中如何实现double和CString两种数据类型的转换?非常感谢! (YuanBing)
将字符转换为数,可以使用atof、_gcvt。例子:
#i nclude <stdlib.h>
#i nclude <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s/t/tinteger: %d/n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s/t/tlong: %ld/n", s, l );
}
而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
s.Format("%d", i)
Format函数的功能很强,值得你研究一下。
yifeijiang的意见:
补充一点:CString转换为Double:
CString str("1.234");
double dbl;
dbl = atof((LPCTSTR)str);
zhouhh的意见:
如果是Unicode,可以采用_tttof,_tttoi这两个函数。
也可以采用_stscanf这个函数来实现由字符串转换到整数。
如何将CString类型的变量赋给char*类型的变量
编号: QA002101
建立日期: 1999年11月25日 最后修改日期: 2004年1月1日
所属类别: C/C++ - 其他方面
操作系统: windows98
编程工具: VC++6.0
问题: 请问如何将CString类型的变量赋给char*类型的变量?(john)
Lu Tao的意见:
CString Msg;
Msg=Msg+"abc";
LPTSTR lpsz;
lpsz = new TCHAR[Msg.GetLength()+1];
_tcscpy(lpsz, Msg);
char * psz;
strcpy(psz,lpsz);
doublehu的意见:
char *ch;
CString str;
ch=(LPSTR)(LPCTSTR)str;
陈啸龙的意见:
CString mCS=_T("cxl");
char mch[20];
memcpy(mch,mCS,20);
李海、阿进的意见:
使用CString::GetBuffer函数。例子:
char *p;
CString str("hello");
p=str.GetBuffer(str.GetLength());
榕溶的意见:
CString aaa="aaa";
char *ch
ch=aaa.c_str;
相关问题:
QA002733 "WriteFile函数中的字符数组能否用CString对象来替代"
longbow问: 反过来怎么办?即如何将char*类型的变量赋给cstring型的变量?
李海:这很容易,可以直接赋值,如:
CString myString = "This is a test";
也可以利用构造函数,如:
CString s1( "Tom" );