cstring和相关类型的相互转换int,double,char*


本文主要解析cstring和int,double,char*之间的相互转换方法:

        cout<<"测试1:int to cstring"<<endl;
	int num=19;
	CString cstr;
	cstr.Format(_T("%4d"),num);
	wcout<<cstr.GetString()<<endl;


	cout<<"测试2:float to cstring"<<endl;
	float f1=19.3234876;
	cstr.Format(_T("%.2f"),f1);
	wcout<<cstr.GetString()<<endl;


	cout<<"测试3:double to cstring"<<endl;
	double d1=19.3234876;
	cstr.Format(_T("%.8lf"),d1);
	wcout<<cstr.GetString()<<endl;
	
	cout<<"测试4:16进制 to cstring"<<endl;
	int num2=10;
	cstr.Format(_T("%x"),num2);
	wcout<<cstr.GetString()<<endl;

	cout<<"测试5:char to cstring"<<endl;
	char ch[10]="中国ABC";
	cstr.Format(_T("%S"),ch);
	wcout<<cstr.GetString()<<endl;

	cout<<"测试6:cstring to int"<<endl;
	CString cstrnum=_T("9987");
	num=_ttoi(cstrnum);
	cout<<num<<endl;

	cout<<"测试7:cstrign to double"<<endl;
	CString cstrdou=_T("987.32673");
	double d2= _wtof(cstrdou);
	cout<<d2<<endl;

	cout<<"测试8:cstrign to char*"<<endl;
	cstr=L"你好ABc";
	string s=Unicode_To_Ansi(cstr.GetString());
	cout<<s<<endl;

CString转std::wstring

std::wstring str = filename.GetString();

std::wstring转CString

CString str( filename.c_str() );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值