LPWSTR字符串转换为布尔?

问题描述 投票:0回答:2

我有一个类型为LPWSTR的变量,其中包含"true""false"。有没有办法用truefalse将其转换为bool类型的变量?

理想情况下,我想做这样的事情:

FooClas::MyMethod()
{
    LPWSTR  variableOne;
    bool    variableTwo;
    MyMethodOne(&variableOne);

    // At this point, variableOne can be either "true" or "false".
    // Do something to check if "true", then variableTwo = true, otherwise variableTwo = false.
}
c++ winapi
2个回答
2
投票

std::wistringstream可以帮助:

std::wistringstream(variableOne) >> std::boolalpha >> variableTwo;

头文件:

#include <sstream>
#include <string>

1
投票

鉴于先决条件,即variableOne指向"true""false",将其转换为布尔值的最有效方法是:

bool const variableTwo { *variableOne == L't' };

足以测试单个区别属性,例如第一个字符。其他任何字符,甚至字符串的长度都足够。

© www.soinside.com 2019 - 2024. All rights reserved.