class GetHashCode:
"""
类似 Java中hashcode函数
java实现:
ID = String.valueOf(Math.abs((this.OMC_IP + this.FTP_PORT + this.FILE_PATH).hashCode()))
"""
def convert_n_bytes(self, n, b):
bits = b * 8
return (n + 2 ** (bits - 1)) % 2 ** bits - 2 ** (bits - 1)
def convert_4_bytes(self, n):
return self.convert_n_bytes(n, 4)
@classmethod
def getHashCode(cls, s):
h = 0
n = len(s)
for i, c in enumerate(s):
h = h + ord(c) * 31 ** (n - 1 - i)
return cls().convert_4_bytes(h)
类似Java中hashcode函数(python)
- 郭良俊只狗
- 0