Search code examples
arraysstringdelphibytedelphi-xe2

assign byte to string delphi xe2


I have code

procedure _UUEncode;
var
  Sg: string;
  Triple: string[3];
begin
  ...
  Byte(Sg[1]) := Byte(Sg[1]) + Length(Triple); // <- on this line I got error
  ...
end;

I got "left sign cannot be assigne to" error, someone can help me? I try to make conversion from Delphi 7 component to XE2 component

thanks for suggestion, I realy appreciated, may be someone have cheklist what I must pay attention while convert delphi7 vcl component to xe2


Solution

  • I would write it like this, in all versions of Delphi:

    inc(Sg[1], Length(Triple));
    

    It's always worth avoiding casts if possible. In this case you are wanting to increment an ordinal value, and inc is what does that.

    The reason your typecast failed is that casts on the target of an assignment are special. These typecasts are known as variable typecasts and the documentation says:

    You can cast any variable to any type, provided their sizes are the same and you do not mix integers with reals.

    In your case the failure is because the sizes do not match. That's because Char is two bytes wide in Unicode Delphi. So, the most literal conversion of your original code is:

    Word(Sg[1]) := Word(Sg[1]) + Length(Triple);
    

    However, it's just better and clearer to use inc.

    It's also conceivable that your uuencode function should be working with AnsiString since uuencode maps binary data to a subset of ASCII. If you did switch to AnsiString then your original code would work unchanged. That said, I still think inc is clearer!