Come faccio a convertire un CString a una doppia in C ++?
-
06-09-2019 - |
Domanda
Come faccio a convertire un CString
ad un double
in C ++?
Il supporto Unicode sarebbe bello anche.
Grazie!
Soluzione
Un CString
può convertire ad un LPCTSTR
, che è fondamentalmente un const char*
(const wchar_t*
in Unicode build).
Sapendo questo, è possibile utilizzare atof()
:
CString thestring("13.37");
double d = atof(thestring).
... o per Unicode costruisce, _wtof()
:
CString thestring(L"13.37");
double d = _wtof(thestring).
... o per supportare sia Unicode e non Unicode si basa ...
CString thestring(_T("13.37"));
double d = _tstof(thestring).
(_tstof()
è una macro che si espande a uno o atof()
_wtof()
basati su o meno _UNICODE
è definito)
Altri suggerimenti
È possibile convertire qualsiasi cosa a qualsiasi cosa con un std::stringstream
. L'unico requisito è che gli operatori >>
e <<
essere attuate. Stringstreams possono essere trovati nel file di intestazione <sstream>
.
std::stringstream converter;
converter << myString;
converter >> myDouble;
con la spinta lexical_cast biblioteca, si fa
#include <boost/lexical_cast.hpp>
using namespace boost;
...
double d = lexical_cast<double>(thestring);