python实现java的hashcod

发布时间:2019-09-02 08:07:17编辑:auto阅读(2077)

    def convert_n_bytes(n, b):
    bits = b * 8
    return (n + 2 ** (bits - 1)) % 2 ** bits - 2 ** (bits - 1)

    def convert_4_bytes(n):
    return convert_n_bytes(n, 4)

    def getHashCode(s):
    h = 0
    n = len(s)
    for i, c in enumerate(s):
    h = h + ord(c) * 31 ** (n - 1 - i)
    return convert_4_bytes(h)

关键字