Raised This Month: $32 Target: $400
 8% 

[USEFUL FUNCTION] StrToBool


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Olly
Senior Member
Join Date: Mar 2007
Location: UK
Old 05-16-2007 , 21:36   [USEFUL FUNCTION] StrToBool
Reply With Quote #1

Code:
/*****************************************************************
 * StrToBool
 *
 * @breif Converts a string (1|0|on|off) to bool
 * @params String:input string bool
 * @return true|false
 *****************************************************************/
bool:StrToBool(String:input[])
{
    if(StrContains(input, "1") != -1 || StrContains(input, "on") != -1)
    {
        return true;
    }
    else if(StrContains(input, "0") != -1 || StrContains(input, "off") != -1)
    {
        return false;
    }
return false;
// YAY!
}
Note for this to work you MUST leave in '// YAY!' without it, it wont compile

I know its a simple function, but meh, saves someone else needing to type it all out

Hope its as usefull as the title makes out

Last edited by Olly; 05-16-2007 at 22:28. Reason: Because i can
Olly is offline
Send a message via MSN to Olly
teame06
i have a hat
Join Date: Feb 2005
Location: Hat City
Old 05-16-2007 , 21:46   Re: [USEFUL FUNCTION] StrToBool
Reply With Quote #2

Code:
bool:StrToBool(String:input[]) {     if(StrContains(input, "1") != -1 || StrContains(input, "on") != -1)     {         return true;     }     else if(StrContains(input, "0") != -1 || StrContains(input, "off") != -1)     {         return false;     } // YAY! /* Otherwise you could get an return warning 209: function "StrToBool" should return a value*/
return  false;
}
__________________
No private support via Instant Message
GunGame:SM Released
teame06 is offline
Send a message via AIM to teame06
Olly
Senior Member
Join Date: Mar 2007
Location: UK
Old 05-16-2007 , 22:29   Re: [USEFUL FUNCTION] StrToBool
Reply With Quote #3

yea i fixed that in my plugin, and totally forgot that i put it here ><
Olly is offline
Send a message via MSN to Olly
L. Duke
Veteran Member
Join Date: Apr 2005
Location: Walla Walla
Old 05-16-2007 , 23:01   Re: [USEFUL FUNCTION] StrToBool
Reply With Quote #4

I understand why the "YAY!" is there but not the "else if"

Code:
bool:StrToBool(String:input[])
{
    if(StrContains(input, "1") != -1 || StrContains(input, "on") != -1)
    {
        return true;
    }

// YAY!
 return  false;
}
L. Duke is offline
BAILOPAN
Join Date: Jan 2004
Old 05-17-2007 , 00:39   Re: [USEFUL FUNCTION] StrToBool
Reply With Quote #5

that parameter should be const
__________________
egg
BAILOPAN is offline
Olly
Senior Member
Join Date: Mar 2007
Location: UK
Old 05-17-2007 , 14:29   Re: [USEFUL FUNCTION] StrToBool
Reply With Quote #6

lol, i cant even get a simple function like this right
Olly is offline
Send a message via MSN to Olly
sslice
Senior Member
Join Date: Feb 2005
Location: Texas, USA
Old 05-19-2007 , 00:24   Re: [USEFUL FUNCTION] StrToBool
Reply With Quote #7

Something like this is really dependent on the input.. could deal with 'true' and 'false', 'T' and 'F', 'yes' and 'no', etc. etc... for ConVars it would just recognize 1 and 0... and the double 'not' trick is used to convert it to boolean (to handle values other than 1 and 0).
Code:
bool:b = !!StringToInt(str);
Just depends on what you are using StrToBool() for in the end.
__________________
sslice is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 05-19-2007 , 13:48   Re: [USEFUL FUNCTION] StrToBool
Reply With Quote #8

Quote:
Originally Posted by sslice View Post
Something like this is really dependent on the input.. could deal with 'true' and 'false', 'T' and 'F', 'yes' and 'no', etc. etc... for ConVars it would just recognize 1 and 0... and the double 'not' trick is used to convert it to boolean (to handle values other than 1 and 0).
Code:
bool:b = !!StringToInt(str);
Just depends on what you are using StrToBool() for in the end.
I think (expression ? true : false) is less cryptic but you decide
__________________
hello, i am pm
PM is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:14.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode