Hello,
I have a Pascal code for some hash function. I've tried to rewrite it on Python but it doesn't give the right value (which is 32202). Please tell me where is the error.

Thanks.

**********************
    Pascal program
**********************

Program K_S;
    Var
      ST: String;
    function KSUM(var S: String): String;
    var
      i, l: Integer;
      KS: Word;
      SS: String;
    begin
      KS:=65535;
      for i:=2 to Length(S) do
      begin
        KS := KS xor Ord(S[i]);
        writeln(S[i]);
        for l:=1 to 8 do
         if (KS div 2)*2<>KS then KS := (KS div 2) xor 40961
         else KS := (KS div 2);
      end;
      Str(KS, SS);
      KSUM := SS;
    end;


    Begin
     ST := ':1;1;2;';
     Writeln(KSUM(ST));
    End.


**********************
    Python program
**********************

def myhash(s):
        KS=65535
        for i in range(1,len(s)):
                KS=KS ^ ord(s[i])
                for j in range(1,8):
                        if (KS // 2)*2 != KS:
                                KS=(KS // 2) ^ 40961
                        else:
                                KS=KS // 2
        return(str(KS))
if __name__ ==  "__main__":
        st=myhash(":1;1;2;")
        print (st)
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to