Java String.hashCode python implementation

The problem

Recently we had to reproduce some java code in python and we faced this :

int i = someString.hashCode()

As we needed the real computed value and not the hash mecanism behind it (which python has as well), we had to reimplement this String.hashCode() in python.

EDIT

A contributor called Janek37 posted in a comment an improved version of my code.

As it does the exact same job and is much faster for long strings (it prevents python from manipulating big numbers), i post it below. Thanks Janek !

Source

Get it here : http://ftp.pimentech.net/src/libcommonPython/src/python/libcommon/javastringhashcode2.py

def java_string_hashcode(s):
    h = 0
    for c in s:
        h = (31 * h + ord(c)) & 0xFFFFFFFF
    return ((h + 0x80000000) & 0xFFFFFFFF) - 0x80000000

Usage Example

>>> java_string_hashcode('Big Bisou')
1477474450

Old source

Get it here : http://ftp.pimentech.net/src/libcommonPython/src/python/libcommon/javastringhashcode.py

It is worth noticing that we had to reimplement 'dumb' int calculus in python, as this smart kid automatically switches from int to long. So we had to reimplement int overflow too. Thanks to this guy it was pretty simple:

>>> convert_4_bytes(2**31-1)
2147483647L

>>> convert_4_bytes(2**31)
-2147483648L

Commentaires

madonna Mai 21, 2010 at 10:17 après-midi

I love your web-site!!!!!

Russ Juillet 8, 2010 at 5:40 après-midi

Thanks, works great!

Comments

blog comments powered by Disqus