使用 Inno Setup 将布尔值转换为字符串

Convert Boolean to String with Inno Setup

在 Inno Setup Pascal 脚本中将布尔值转换为字符串的最简单方法是什么?这个应该完全隐式的琐碎任务似乎需要一个完整的 if/else 构造。

function IsDowngradeUninstall: Boolean;
begin
    Result := IsCommandLineParamSet('downgrade');
    MsgBox('IsDowngradeUninstall = ' + Result, mbInformation, MB_OK);
end;

这不起作用,因为 "Type mismatch"。 IntToStr 也不接受 BooleanBoolToStr 不存在。

[Code]
function BoolToStr(Value : Boolean) : String; 
begin
  if Value then
    result := 'true'
  else
    result := 'false';
end;

[Code]
function IsDowngradeUninstall: Boolean;
begin
    Result := IsCommandLineParamSet('downgrade');
    if Result then 
      MsgBox('IsDowngradeUninstall = True', mbInformation, MB_OK)
    else
      MsgBox('IsDowngradeUninstall = False', mbInformation, MB_OK);
end; 

如果只需要一次,最简单的内联解决方案是将 Boolean 转换为 Integer 并使用 IntToStr functionTrue 得到 1False 得到 0

MsgBox('IsDowngradeUninstall = ' + IntToStr(Integer(Result)), mbInformation, MB_OK);

不过,我通常使用 Format function 来获得相同的结果:

MsgBox(Format('IsDowngradeUninstall = %d', [Result]), mbInformation, MB_OK);

(与 Delphi 相反)Inno Setup/Pascal 脚本 Format%dBoolean 隐式转换为 Integer


如果您需要更花哨的转换,或者如果您经常需要转换,请实现您自己的功能,正如@RobeN 已经在他的回答中显示的那样。

function BoolToStr(Value: Boolean): String; 
begin
  if Value then
    Result := 'Yes'
  else
    Result := 'No';
end;