如何在wchar_t*和int之间进行转换?

Chr*_*ris 2 c++ casting

我有一个函数,它返回xml元素的内部文本.然而,它返回它作为一个const wchar_t*.我希望将此值作为整数返回(在其他一些情况下为浮点数).这样做的最佳方法是什么?

Tim*_*ter 7

C++的方式是:

wchar_t* foo = L"123";
std::wistringstream s(foo);
int i = 0;
s >> i;
Run Code Online (Sandbox Code Playgroud)

使用Boost,您可以:

try {
    int i2 = boost::lexical_cast<int>(foo);
} catch (boost::bad_lexical_cast const&) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

根据您使用的CRT实现,您可能具有"广泛" atoi/ strtol功能:

int i = _wtoi(foo);
long l = _wcstol(foo, NULL, 10);
Run Code Online (Sandbox Code Playgroud)