Board index » delphi » Encrypt example does not work?

Encrypt example does not work?

Hi All,

Here is a snippet of code from Borland's site:

function TForm1.Encrypt(const s: string; key: Word): string;
var
  I: byte;
begin
  Result[0] := S[0];
  for I := 1 to Length(S) do begin
    Result[I] := char(byte(S[I]) xor (Key shr 8));
    Key := (byte(Result[I]) + Key) * C1 + C2;
  end;
end;

which always returns the following error in Delphi5:

Element 0 inaccessible - use 'Length' or 'SetLength'

Fine. However, if you use Length / SetLength on
Result, you get another error!!

Is it me, or am I not seeing how to recode this
properly!!

Any help would be appreciated.

all the best,

Mark.

 

Re:Encrypt example does not work?


Try to use ShortString instead String type:
function TForm1.Encrypt(const s: ShortString; key: Word): string;
...

--
With best regards, Mike Shkolnik
E-Mail: mshkol...@scalabium.com
        mshkol...@yahoo.com
WEB: http://www.scalabium.com

Mark O'Neill <nos...@none.no> D??? ???Y??:3c8352c5_1@dnews...

Quote
> Hi All,

> Here is a snippet of code from Borland's site:

> function TForm1.Encrypt(const s: string; key: Word): string;
> var
>   I: byte;
> begin
>   Result[0] := S[0];
>   for I := 1 to Length(S) do begin
>     Result[I] := char(byte(S[I]) xor (Key shr 8));
>     Key := (byte(Result[I]) + Key) * C1 + C2;
>   end;
> end;

> which always returns the following error in Delphi5:

> Element 0 inaccessible - use 'Length' or 'SetLength'

> Fine. However, if you use Length / SetLength on
> Result, you get another error!!

> Is it me, or am I not seeing how to recode this
> properly!!

> Any help would be appreciated.

> all the best,

> Mark.

Re:Encrypt example does not work?


Quote
"Mike Shkolnik" <mshkol...@yahoo.com> wrote in message

news:3c8356bb_1@dnews...

Quote
> Try to use ShortString instead String type:
> function TForm1.Encrypt(const s: ShortString; key: Word): string;

No, that does not work Mike. I know what you're getting
at though...

Thanks anyway. The problem lies in the fact that
Delphi5 does not let you directly access element 0 since
it does not store the length of the string there anymore...

All the best,

Mark.

Re:Encrypt example does not work?


What do you mean "if you use SetLength on Result you get another error"? I
use
SetLength(Result, x) in several of my routines, and it works fine:

function SetRes(Input: string): string;
begin
  SetLength(Result, Length(Input));
  ShowMessage('Length of Result is ' + IntToStr(Length(Result)));
end;

Ray

Quote
"Mark O'Neill" <nos...@none.no> wrote in message news:3c835ebe_1@dnews...
> "Mike Shkolnik" <mshkol...@yahoo.com> wrote in message
> news:3c8356bb_1@dnews...
> > Try to use ShortString instead String type:
> > function TForm1.Encrypt(const s: ShortString; key: Word): string;

> No, that does not work Mike. I know what you're getting
> at though...

> Thanks anyway. The problem lies in the fact that
> Delphi5 does not let you directly access element 0 since
> it does not store the length of the string there anymore...

> All the best,

> Mark.

Re:Encrypt example does not work?


Quote
"Ray Cernis" <cern...@westonedesign.com> wrote in message

news:3c836113$1_1@dnews...

Quote
> What do you mean "if you use SetLength on Result you get another error"? I
> use
> SetLength(Result, x) in several of my routines, and it works fine:

Ray,

You are absolutely right. Before reading your post, I tried:

SetLength(Result,Length(s));

again, and it worked fine - I think I'm going nuts!!

Thanks anyway.

All the best,

Mark.
~~ we have days like these you know! ~~

Other Threads