4
votes

I'm using Inno Setup and want to check with Pascal Script if a string variable is an Integer (0-9 only, no hex). I have made this funcion:

function IsInt(s: string): boolean;
var
  i, len: Integer;
begin
  len := length(s);

  if len = 0 then
    result := false
  else
  begin
    result := true;
    for i := 1 to len do
    begin
      if not (s[i] in ['0'..'9']) then  !!! ERROR HERE !!!
      begin
        result := false;
        exit;
      end;
    end;
  end;
end; 

But the compiler raises an error:

Closing square bracket (']') expected.

How to fix it?

If I change the line to this:

  if not (s[i] in ['0','1','2','3','4','5','6','7','8','9']) then

It complies but if the code is executed it gives this error:

Runtime Error - Invalid Type.

What to do?

2
Looks like you want to check if a String is a Number, not Integer. Am I right? Because you don't need it to Return True when it passes a HEX Number.GTAVLover

2 Answers

3
votes

Rather than using sets you could just do a simple range test, e.g.

IF (s[i] < '0') OR (s[i] > '9') THEN
   ...
2
votes

From the Pascal script documentation

Prototype: function StrToIntDef(s: string; def: Longint): Longint;

Description: The StrToInt function converts the string passed in S into a number. If S does not represent a valid number, StrToInt returns the number passed in Def.

so set def to -1 and if your string is not a number it will return -1.