this slowpoke moves

Calculate Java Hash

uses Math

//

function hashCode(val: string): Integer;
var
  i: Integer;
  res: Extended;

  function RoundEx(x: Extended): Integer;
  begin
    Result := Trunc(x) + Trunc(Frac(x) * 2);
  end;
begin
  res := 0;

  for i := 1 to Length(val) do
  begin
    res := res + Ord(val[i]) * Power(31, Length(val) - (i - 1) - 1);
  end;

  Result := RoundEx(res);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := IntToStr(hashCode('hallo'));
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate