Converting old Delphi key generator

Hi All,
Here is a bit of code I found ages ago.
Could anyone tell me what to do with the ORD function bit.

function GetRegString(Company, ABN,rType: string): string;
var
  S,F,P : string;
  bdate, Key, xKey :string;
  I, N, PN: LongInt;
begin

  N:=0;

  F:=rType;

  S:=Company + ABN + F
 
  S:= uppercase(S);
  
  for i:=1 to length(S) do
    begin
      if i <> 1 then Pn:=ord(S[i-1]) else Pn:=ord(S[1]);
        N:=Pn*(N+ord(S[i]));
    end;
  N:=N;
  S:=IntToHex(N, 8);
  while S[1] = '0' do Delete(S, 1, 1);
  Result:=F + '-' + S;
end;

I assume the ord is a bitwise type rotation, but to hard for little old me. lol

Tom

Given the code, I think the closest equivalent is the Asc function. Something like:

if i <> 1 then
  Pn = S.Mid( i -1, 1 ).Asc
else
  Pn = S.Left( 1 ).Asc
end if
N = Pn * ( N + S.Mid( i, 1 ).Asc )

Thanks for that will test it.

Once you’ve got it working, post the code here. If you translate the Delphi code to Xojo as written, it will be slower than it needs to be.

Thanks for that.
Got it working.


Method  GenKey (abn as string , name as string) as string

  dim i,n,PN as integer
  dim F,S as string
  
  N=0
  
  F ="NAME"
  
  S = name + abn + F
  S = uppercase(S)
  
  for i = 1 to len(S)
    if i <> 1 then
      Pn = S.Mid( i -1, 1 ).Asc
    else
      Pn = S.Left( 1 ).Asc
    end if
    N = Pn * ( N + S.Mid( i, 1 ).Asc )
  next
  
  S = hex(N) 
  
  Return  S
  

Works well enough for me, thanks
Tom

That’s good. My only suggestion is to cache the value for Len(S). Each iteration of the loop will calculate that again and, if your strings are long, you’ll wonder why it’s taking so much time.

...
dim lastCharIndex as integer = S.Len
for i = 1 to lastCharIndex
…

Put the string in a memoryblock and use the Byte() accessor to manipulate the characters. Or at least use MidB/LeftB/LenB to get at single byte values, not characters which could be multi-byte.