[fpc-pascal] BoolToStr
Michael Van Canneyt
michael at freepascal.org
Sun Aug 28 17:39:21 CEST 2022
On Sun, 28 Aug 2022, James Richters via fpc-pascal wrote:
> I'm generating a report where I wish to display some Boolean values. I
> thought I would try BoolToStr, as according to:
> https://www.freepascal.org/docs-html/rtl/sysutils/booltostr.html it states:
>
> Description
> BoolToStr converts the boolean B to one of the strings 'TRUE' or 'FALSE'
>
> So it should do exactly what I want. output the text string 'TRUE' or
> 'FALSE' depending on the Boolean state,
> but that is not what happens:
>
> Uses Sysutils;
> Begin
> Writeln(BooltoStr(True));
> Writeln(BooltoStr(False));
> End.
>
> Running "i:\booltostr.exe "
> -1
> 0
>
> Why true is -1 instead of 1 is beyond me, but anyway, I would consider this
> BoolToInt, not BoolToStr, I want the Strings 'TRUE' or 'FALSE' as indicated
> in the documentation
I will adapt the documentation to be more precies.
You can use
BoolToStr(b,'TRUE','FALSE')
or
BoolToStr(b,True)
in which case it will use the BoolStrs arrays to determine the strings.
-1 and 0 are the Delphi values, which, I agree, are not very intuitive.
>
> And BoolToInt should always output 0 or 1, not matter what operating system
> you are on.. I don't understand the -1. I guess some Windows API's use -1
> for true.. I still don't understand.
> Maybe there should be a BoolToBit that could always be 0 or 1 regardless of
> the operating system.
As said, the 0 and -1 are for Delphi compatibility.
Use
Ord(Boolean)
It will give you 0 and 1.
Michael.
More information about the fpc-pascal
mailing list