如何将CString转换为整数和浮点数? [英] How to convert CString to integer and float?

查看:927
本文介绍了如何将CString转换为整数和浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将CString转换为int和float,但是无法找到任何C ++库函数来完成此操作.请帮忙.

I am trying to convert CString to int and float but unable to find any C++ library function to get this done. please help.

推荐答案

MFC 中,符合 UNICODE 的正确方法如下:

The proper UNICODE-compliant way of doing it in MFC is the following:

CString sInt = _T("10");
int n = _ttoi(sInt);

CString sFloat = _T("10.1");
float f = _ttof(sFloat);

正如David Heffernan所提​​到的:如果您的项目配置仅是UNICODE,并且您不使用MBCS并且没有针对Windows 98之类的旧MS OS的计划,则可以使用:

As David Heffernan mentioned: If your project configuration is UNICODE only and you don't use MBCS and do not have any plans to target old MS OSs like Window 98 you can use:

CStringW s = L"10";
int i = _wtoi(s); 

C ++ 11 中,您可以使用以下内容:

In C++11 you can use the following:

std::string sInt = "10";
int i = std::stoi(sInt);

std::string sFloat = "10.1";
double d = std::stod(sFloat);

这篇关于如何将CString转换为整数和浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆