Why delphi BoolToStr true is represented as -1

  • A+
Category:Languages

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?

 


The source of these particular values is surely down to the 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 reasonable 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:

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: