如何将CString转换为整数和浮点数?

Ana*_*s90 2 c++ windows winapi mfc

我试图转换CString为int和float但无法找到任何C++库函数来完成这项工作.请帮忙.

And*_*gin 6

正确的UNICODE在做这件事的方式兼容MFC如下:

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

CString sFloat = _T("10.1");
float f = _ttof(sFloat);
Run Code Online (Sandbox Code Playgroud)

正如David Heffernan所提​​到的:如果你的项目配置只是UNICODE并且你没有使用MBCS并且没有任何计划针对像Windows 98这样的旧MS操作系统,你可以使用:

CStringW s = L"10";
int i = _wtoi(s); 
Run Code Online (Sandbox Code Playgroud)

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

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

std::string sFloat = "10.1";
double d = std::stod(sFloat);
Run Code Online (Sandbox Code Playgroud)