Author Topic: StrtoBool & BooltoStr (Add-On)  (Read 5929 times)

0 Members and 1 Guest are viewing this topic.

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
StrtoBool & BooltoStr (Add-On)
« on: December 24, 2007, 09:28:30 pm »
I'd put this in a zip, but it's a cut-and-paste-in-your-script kind of script (an add-on)

Script Name: StrtoBool & BooltoStr
Script Description:
StrtoBool: changes strings to booleans (you can add more valid strings in the case list)
BoolToStr: changes Booleans to strings (just true and false to 'true' and 'false')
Original Author(s): Kavukamari
Core Version: 2.6.3

Valid strings(current list):
'true','on','active','yes','enable','enabled','hi','hello' (returns true)
'false','off','inactive','no','disable','disabled','bye','goodbuy','goodbye' (returns false)
(I added goodbye)

Directions: paste script after your vars, consts, and types (before your first event)

Code:
Code: [Select]
const
  D_Num=1;

var
  NumType: integer;

function strtobool(str: string; def: boolean):boolean;
begin
  Result:=def;
  try
    NumType:=D_Num;
    Case NumType of
      1: begin //Kavukamari
        if (strtoint(str) > 0) then Result:=true else if (strtoint(str) < 1) then Result:=false;
      end;
      2: begin //DorkeyDear
        if (strtoint(str) = 1) then Result:=true else Result:=false;
      end;
      3: begin //PerroAZUL/php
        if (strtoint(str) = 0) then Result:=false else Result:=true;
      end;
    end;
  except
    Case str of
      'true','on','active','yes','enable','enabled','hi','hello': begin
        Result:=true;
      end;
      'false','off','inactive','no','disable','disabled','bye','goodbuy','goodbye': begin
        Result:=false;
      end;
    end;
  end;
end;

function booltostr(bool: boolean):string;
begin
  Case bool of
    true: Result:='true';
    false: Result:='false';
  end;
end;
« Last Edit: December 28, 2007, 10:34:05 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: StrtoBool & BooltoStr (Add-On)
« Reply #1 on: December 24, 2007, 10:07:07 pm »
Lol, I JUST made this in one of my scripts, the StrtoBool function :P
for your try thing, i would make it so if str = 1, then true, if any other #, false.. isn't that what is used in other programming/scripting languages that don't have booleans?
one thing I had different was this..
StrtoBool(Text: string; Default: boolean): boolean;
Default will be the result if Text is none of the true or false checks.
You should implement it also if you want..

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: StrtoBool & BooltoStr (Add-On)
« Reply #2 on: December 24, 2007, 10:10:16 pm »
I might... I never thought of that :P

about the numbers, I like pos/neg numbers more than if = 1 then true else false
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: StrtoBool & BooltoStr (Add-On)
« Reply #3 on: December 26, 2007, 04:24:50 am »
goodbuy!
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: StrtoBool & BooltoStr (Add-On)
« Reply #4 on: December 26, 2007, 07:05:21 pm »
goodbuy!

??? am i the weakest link?

or did I break one of the laws of coding? (should 2 or 3 be false? 4? 5? 7.14572?)
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: StrtoBool & BooltoStr (Add-On)
« Reply #5 on: December 26, 2007, 11:17:43 pm »
It's 'goodbye' not 'goodbuy' ;)
Anyway, hello/hi/bye/goodbye don't look like booleans at all to me.
And about the numbers, in php "0" would be false and any other number would be true. And that makes a lot of sense to me.
urraka

Offline KwS Pall

  • Major(1)
  • Posts: 49
  • I'm going to write Tibia for Soldat
Re: StrtoBool & BooltoStr (Add-On)
« Reply #6 on: December 27, 2007, 08:13:40 am »
yea for me 2.
generally good script
I'm going to write Tibia for Soldat

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: StrtoBool & BooltoStr (Add-On)
« Reply #7 on: December 28, 2007, 10:32:05 pm »
UPDATE: added the three variations to the number part of the script, added Goodbye to the list, but kept goodbuy for misspellurs like mi
« Last Edit: December 28, 2007, 10:38:19 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: StrtoBool & BooltoStr (Add-On)
« Reply #8 on: December 29, 2007, 10:47:20 am »
valid + invalid

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: StrtoBool & BooltoStr (Add-On)
« Reply #9 on: December 29, 2007, 11:55:01 am »
Generally speaking, in programming languages, there are the following typical ways of handling true/false in terms of numbers:

C/C++/PHP: 0 -> false, !=0 -> true
negative numbers are treated as true if signed integers are used

Some other languages(VB and Delphi I believe): 0 -> false, bitwise NOT of 0 = true, anything else -> undefined
value depends on the type used to hold the boolean; a signed integer means that true will always be equal to -1 no matter the size of the integer, which is usually the case

Yet other languages: 0 -> false, 1 -> true, anything else -> undefined
Similar to above, but does bitwise NOT on a single bit only

So, to sum up, there are:

F/T
----
0/1
0/-1
0/x
« Last Edit: December 29, 2007, 11:58:12 am by chrisgbk »

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: StrtoBool & BooltoStr (Add-On)
« Reply #10 on: December 29, 2007, 02:33:31 pm »
that seems really confusing, especially the "bitwise NOT of 0 = true" part and the second part of the part about VB and Delphi...

but if you understand what you're saying, you can always add it to the case statement ;)
« Last Edit: December 29, 2007, 02:37:04 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: StrtoBool & BooltoStr (Add-On)
« Reply #11 on: December 29, 2007, 10:41:52 pm »
I would make it 0=false 1=true and anything else, it doesn't matter, its whatever the default is set to...

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: StrtoBool & BooltoStr (Add-On)
« Reply #12 on: December 29, 2007, 11:16:40 pm »
should I add 0 = false, 1 = true, <> = Nan ?
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: StrtoBool & BooltoStr (Add-On)
« Reply #13 on: December 30, 2007, 10:20:32 pm »
that seems really confusing, especially the "bitwise NOT of 0 = true" part and the second part of the part about VB and Delphi...

but if you understand what you're saying, you can always add it to the case statement ;)

Huge long explanation time.

A bitwise NOT is an inversion of the bits of a binary number, ie for the 0 and -1 implementation:

Code: [Select]
NOT 00000000 (false)
       =======
       11111111 (true)

NOT 11111111 (true)
       =======
       00000000 (false)

Treating those numbers as 2's compliment signed integers results in false being equal to 0, and true being equal to -1. This is true regardless of the number of bytes used to store the boolean, hence why it is popular. This implementation allows you to easily negate values.

If unsigned integers were used, the actual value of the number would depend on the number of bytes used to store the boolean; 1 byte would have a true value of 255, 2 bytes would be 65535, 4 bytes would be 4294967295, which would be a pain to deal with.



The other implementation, with 0 and 1, is less used because the binary number only uses 1 bit of however many there are, meaning you can't use NOT directly:

Code: [Select]
NOT 00000000 (false)
       =======
       11111111
AND 00000001
       =======
        00000001 (true)

NOT 00000001 (true)
       =======
       11111110
AND 00000001
       =======
        00000000 (false)

or, alternately, and usually more efficient:

Code: [Select]
       00000000 (false)
XOR 00000001
       =======
       00000001 (true)

       00000001 (true)
XOR 00000001
       =======
       00000000 (false)

This method isn't often used because of the complexity involved, as you can see.



The C/C++ method can't use  NOT directly, since any number other than 0 is true; see the following:

Code: [Select]
NOT 10101010 (true)
       =======
       01010101 (true)

So instead, C/C++ either relies on comparison, or internally translates values to an internal boolean representation like one of the above; there is 1 value for false, and ((2^n)-1) values for true, ranging from 1 to ((2^n)-2), where n is the number of bits used to store the boolean value.

Basically, if a given value is equal to 0, it's false; otherwise, it's true.



Hopefully those of you who bothered to read this learned something.  :D

tl;dr version:

There are 3 primary representations of false and true as numbers: 0 and 1, 0 and -1, 0 and x, where x is any number.