Domanda

Come faccio a convertire un CString ad un double in C ++?

Il supporto Unicode sarebbe bello anche.

Grazie!

È stato utile?

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);

strtod (o wcstod ) permette di convertire le stringhe ad un valore a doppia precisione.

(Richiede <stdlib.h> o <wchar.h>)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top