delphibooleanpascal

Why delphi BoolToStr true is represented as -1


Delphi function BoolToStr converts boolean value to a string.

The result is either true or false, or 'numeric', '-1' and '0' respectively. Why -1 and not 1?


Solution

  • The source of these particular values is surely down to 0 and -1 being the values used by the COM boolean type.

    Certainly in older versions of the Delphi RTL this function was used when converting variants from one type to another, so I'd be reasonably confident that COM variant support was the reason behind this decision.

    You can see the remnants of that original code today in VariantChangeSimpleIntoSimple found in System.VarUtils. When asked to convert varBoolean to varOleStr it does:

    VarOleStrFromStr(Dest, BoolToStr(LSource.VBoolean))
    

    Further reading: