如何转换一个CString为双在C ++?
-
06-09-2019 - |
题
如何转换CString
在C ++一个double
?
支持Unicode也将是很好的。
谢谢!
解决方案
一个CString
可以转换为LPCTSTR
,这基本上是一个const char*
(以Unicode const wchar_t*
构建)。
知道了这一点,可以使用 atof()
:
CString thestring("13.37");
double d = atof(thestring).
...或Unicode版本, _wtof()
:
CString thestring(L"13.37");
double d = _wtof(thestring).
...或同时支持Unicode和非Unicode版本...
CString thestring(_T("13.37"));
double d = _tstof(thestring).
(_tstof()
是扩展为atof()
或_wtof()
基于_UNICODE
是否被定义的宏)
其他提示
您可以的的东西转换到任何的使用std::stringstream
。唯一的要求是,运营商>>
和<<
实施。 Stringstreams可以在<sstream>
头文件中找到。
std::stringstream converter;
converter << myString;
converter >> myDouble;
与升压lexical_cast的图书馆,你做
#include <boost/lexical_cast.hpp>
using namespace boost;
...
double d = lexical_cast<double>(thestring);
不隶属于 StackOverflow