“colon (':') expected” compiler error on c

2019-01-27 03:38发布

问题:

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?

回答1:

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).


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.