如何将TCHAR转换为布尔值 [英] How to TCHAR to Boolean values

查看:102
本文介绍了如何将TCHAR转换为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI
朋友,我需要将TCHAR转换为布尔值,例如

HI
Friends i need to convert TCHAR to Boolean value for example

_TCHAR gstrEnableproxy="false"
o/p:
boolean x=false



希望将其用作推杆

我写的代码是




want this as an Out Put

and the code i have written is


void convertStringBoolean(String* TCHAR)
{
    bool boolVal = false;
    try
    {
        boolVal = system::Convert::ToBoolean(TCHAR);
    }
    catch(System::FormatException*)
    {
        System::Console::WriteLine("The string TCHAR* System::Boolean::TrueString or System::Boolean::flasestring");
    }
}




请帮忙.

预先感谢




please help.

Thanks in advance

推荐答案

0)将String参数更改为更合适的参数,例如str. String在某些C ++名称空间中是一个kweyword.

1)当您可以这样做时,为什么还要编写一个函数:

0) Change the String parameter to something more appropriate, like str. String is a kweyword in some C++ namespaces.

1) Why write a function when you can just do this:

bool x = (str == 'true') ? false : true;



当然,您可能应该将str转换为小写字母以进行比较,以确保捕获到单词"true"的所有排列,但是除非您想将该代码放入一个函数,否则就不需要一个函数.



Of course, you should probably convert str to lowercase for the comparison to make sure you catch all permutations of the word "true", but there''sno need for a function unless you want to put that code into one.


这里有一些基于您在其他回复中的注释的示例代码.

Here''s some sample code based on your comment in the other reply.

// assuming old style MC++ syntax

bool ConvertToStringBoolean(String* input)
{
    return input->ToLower() == "true";
}

// Usage

bool myBool = ConvertToStringBoolean("True"); // myBool will be true

myBool = ConvertToStringBoolean("FALSE"); // myBool will be false


好通过上面的对话,我首先告诉你,解决此问题的方法是Aarti2010女士试图获取一些字符数据,并试图通过打印一些布尔结果来检查数据是否存在.

我不是VC ++人士,但我擅长C#,所以Aarti2010女士请参考下面提供的代码,并告诉我它是否有效,我也给ua
Ok looking at the above conversations let me tell u first that the way have got this problem is that Ms.Aarti2010 is trying to get some character data and is trying to check whether data is present or not via printing some boolean result.

I am not a VC++ guy but i am good in C# so Ms.Aarti2010 please refer to my code given below and do let me know whether it has worked or not also i am giving u a link which may b helpful.

In my code i am accepting some data from the user and then i have a function which will return me the bool result
  bool ToBoolean()
 {
     char ch;
     ch = Convert.ToChar(Console.Read()); //grab a character
     Console.WriteLine(ch); //print the char data
    return (ch > 0)?true:false;
  }

    void bool_value_Checker()
{
    bool b = ToBoolean();
    Console.WriteLine(b);
}


如果您希望将char变量ch声明为全局变量,并尝试不给它任何值,然后运行我的函数,例如在用户未正确输入值的情况下在您的代码中使用它们的may ba方案,那么我希望您能找到这很有帮助.

如果现在仍然觉得代码没有用,那么我建议您看一下我提供的链接
快乐编码:-\


If u want you can declare the char variable ch as a global one and try not giving it any value and then run my functions, like their might b a scenario in ur code when the user does not enter the values right,so i hope you find this helpful.

And if still now you feel that the code was not useful then i will suggest you see the link that i gave provided
Happy Coding :-\


这篇关于如何将TCHAR转换为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆