"colon (':') expected" Inno Setup Pascal 脚本中 case 语句中字符范围的编译器错误

"colon (':') expected" compiler error on character range in case statement in Inno Setup Pascal script

我在这段代码(第 14 行;第 10 列)中遇到 "colon (:) expected" 语法错误,我不知所措。这段代码在 Inno Setup 编译器中运行,它是 Delphi-like,但我不认为它是完整的 Delphi.

Inno Setup版本是5.5.9(a),所以是Ansi版本。

procedure HexToBin(const Hex: string; Stream: TStream);
var
  B: Byte;
  C: Char;
  Idx, Len: Integer;
begin
  Len := Length(Hex);
  If Len = 0 then Exit;
  If (Len mod 2) <> 0 then RaiseException('bad hex length');
  Idx := 1;
  repeat
    C := Hex[Idx];
    case C of
      '0'..'9': B := Byte((Ord(C) - '0') shl 4);
      'A'..'F': B := Byte(((Ord(C) - 'A') + 10) shl 4);
      'a'..'f': B := Byte(((Ord(C) - 'a') + 10) shl 4);
    else
      RaiseException('bad hex data'); 
    end; 
    C := Hex[Idx+1];
    case C of
      '0'..'9': B := B or Byte(Ord(C) - '0');
      'A'..'F': B := B or Byte((Ord(C) - 'A') + 10);
      'a'..'f': B := B or Byte((Ord(C) - 'a') + 10);
    else
      RaiseException('bad hex data'); 
    end; 
    Stream.WriteBuffer(B, 1);
    Inc(Idx, 2);
  until Idx > Len;
end;

begin
  FStream := TFileStream.Create('myfile.jpg', fmCreate);
  HexToBin(myFileHex, FStream);
  FStream.Free;
end;

有人能发现我的错误吗?

Inno Setup 的 Ansi 版本似乎不支持 case 语句中的范围。

所以你要枚举集合:

case C of
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': B := ...;
  ...
end;

在什么情况下最好使用 if:

if (C >= '0') and (C <= '9') then

虽然更好,但使用 Inno Setup 的 Unicode 版本。现在是 21 世纪,您不应该再开发非 Unicode 应用程序了。参见 。 Inno Setup 6 无论如何只有 Unicode 版本。


你最好使用 CryptStringToBinary Windows API function for the hex to binary conversion anyway. See my answer to your other question .


请注意,您的代码还有很多其他问题。

  • 您正在从 integer 中减去 char
  • Inno Setup 没有 Inc.
  • 的两个参数重载
  • TStream.WriteBuffer 需要 string,而不是 byte