Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

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

I'm getting a "colon (:) expected" syntax error on this code (Line 14; Column 10) and I'm at a loss. This code runs in Inno Setup compiler, it is Delphi-like, but I don't think it is full Delphi.

The Inno Setup version is 5.5.9 (a), so Ansi version.

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;

Can anybody spot my error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Ansi version of Inno Setup does not seem to support ranges in case statement.

So you have to enumerate the set:

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

In what case it's probably better to use if:

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

Though even better, use the Unicode version of Inno Setup. It's 21st century, you should not develop non-Unicode applications anymore. See Upgrading from Ansi to Unicode version of Inno Setup (any disadvantages). And Inno Setup 6 has Unicode version only anyway.


You better use the CryptStringToBinary Windows API function for the hex to binary conversion anyway. See my answer to your other question Writing binary file in Inno Setup.


Note that there are lot of other problems with your code.

  • You are subtracting char from integer.
  • The Inno Setup does not have a two argument overload of Inc.
  • TStream.WriteBuffer takes string, not byte.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...