java hashcode的Delphi实现

程序使用java做后台服务,数据处理时使用了java的hashcode,Delphi程序也需要生成这个hashcode,为了一致,所以要在Delphi下实现和Java一样的算法。

下面即Delphi版的hashCode:

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

  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;
原文地址:https://www.cnblogs.com/GarfieldTom/p/3783503.html