Java String.hashCode python implementation
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 !
def java_string_hashcode(s): h = 0 for c in s: h = (31 * h + ord(c)) & 0xFFFFFFFF return ((h + 0x80000000) & 0xFFFFFFFF) - 0x80000000
>>> java_string_hashcode('Big Bisou') 1477474450
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