質問

私はC ++でCStringdoubleを変換する方法を教えてください。

Unicodeサポートもいいだろう。

ありがとうございます。

役に立ちましたか?

解決

CStringは、基本的に(ユニコードでLPCTSTRビルド)const char*ある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);

にstrtodする (または wcstodする)倍精度値に文字列を変換します。

<stdlib.h>又は<wchar.h>が必要)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top