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.


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 !


Get it here :

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')

Old source

Get it here :

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)

>>> convert_4_bytes(2**31)


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!


blog comments powered by Disqus