Inno Setup - 用零将字符串填充到特定长度

Inno Setup - Pad a string to a specific length with zeros

这是我的代码目前的样子:

var
  Page: TInputQueryWizardPage;

procedure IDKeyPress(Sender: TObject; var Key: Char);
var
  KeyCode: Integer;
begin
  KeyCode := Ord(Key);
  if not ((KeyCode = 8) or ((KeyCode >= 48) and (KeyCode <= 57))) then
    Key := #0;
end;

Procedure InitializeWizard();
Begin
Page := CreateInputQueryPage(blahblah);
  Page.Add('Profile ID:', False);
  Page.Edits[0].MaxLength := 16;
  Page.Edits[0].OnKeyPress := @IDKeyPress;
  Page.Values[0] := '0000000000000000';
End;

procedure WriteUserInput;
var
A: AnsiString;
U: String;
begin
    LoadStringFromFile(ExpandConstant('{app}\prefs.ini'), A);
    U := A;
    StringChange(U, '0000000000000000', Page.Values[0]);
    A := U;
    SaveStringToFile(ExpandConstant('{app}\prefs.ini'), A, False);
end;

procedure CurStepChanged(CurStep: TSetupStep);
Begin
if  CurStep=ssPostInstall then
  begin
    WriteUserInput;
  end
End;

现在我需要 Inno 做的是让用户输入保持原样,如果它已经是 16 位数字,则在末尾填充 0,如果它小于 16(例如,如果它是 15 位数字,则只有一个 0,两个,如果是 14,等等)。什么功能可以做到这一点?

right-padding 具有给定字符的特定长度的字符串的通用函数:

function PadStr(S: string; C: Char; I: Integer): string;
begin
  Result := S + StringOfChar(C, I - Length(S));
end;

对于您的特殊需要,请使用:

StringChange(U, '0000000000000000', PadStr(Page.Values[0], '0', 16));

如果您需要 compile-time 上的填充(在预处理器中),请参阅