title: Convert Delphi BufferToHex function to Python
body: <p>I am just learning Python. Apologies in advance.</p>
  
  <p>I have a Delphi function that creates a string hash.  It first converts a string into an ELF Hash(??) then changes that to a HEX number.</p>
  
  <p>I have the first part working in Python</p>
  
  <pre><code>def ELFHash(key):
      hash = 0
      x    = 0
      for i in range(len(key)):
        hash = (hash &lt;&lt; 4) + ord(key[i])
        x = hash &amp; 0xF0000000
        if x != 0:
          hash ^= (x &gt;&gt; 24)
        hash &amp;= ~x
      return hash
  </code></pre>
  
  <p>In Delphi an additional step is done which converts it to the Hex value</p>
  
  <pre><code>function BufferToHex(const Buf; BufSize : Cardinal) : string;
  var
    I     : LongInt;
  begin
    Result := '';
    for I := 0 to BufSize - 1 do
      Result := Result + IntToHex(TByteArray(Buf)[I], 2); 
  end;
  </code></pre>
  
  <p>buf here is the elf hash i got, stored in a Delphi longint and then BufSize is the Delphi sizeOf() of that longint, which so far seems to return 4.</p>
  
  <p>How would I make a Python function similar to the BufferToHex one that will return an equivalent output? As far as I can tell the python types are different and not returning the same byte size (it seems to return 16 not 4), and when I messed with the ctypes stuff I was still getting different numbers.</p>
  
  <p>Any advice is appreciated. Thanks.</p>