python - 128 位整数哈希函数

标签 python mysql hash bigint

寻找一个字符串到整数散列函数,其值在 mysql bigint unsigned 范围内数据类型(0 <= n <= 18446744073709551615)。将 md5/sha1 转换为以 16 为底的整数不符合此要求。

最佳答案

Java 使用 rolling hash那应该适合你

来自 java.lang.String :

public int hashCode() {
    int h = hash;
    if (h == 0 && count > 0) {
        int off = offset;
        char val[] = value;
        int len = count;

        for (int i = 0; i < len; i++) {
            h = 31*h + val[off++];
        }
        hash = h;
    }
    return h;
}

想法是将哈希计算为:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

要处理溢出,您可以添加一个步骤,根据 18446744073709551615 检查散列,如果它更大,则采用散列的 mod18446744073709551615

关于python - 128 位整数哈希函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17782744/

相关文章:

Python ctypes.WinDLL 错误,找不到 _dlopen(self._name, mode)

Python 相当于 PHP 的空合并运算符和速记三元运算符?

mysql - 登录node js api后无法返回响应

perl - 遍历 perl 中的哈希值

Ruby:反转散列以保留非唯一值

algorithm - 将任意长度的位向量压缩/散列到定义的长度

python - 微 Controller 和pyserial之间的双向通信

python - 从 Python 中的字符串中删除除字母数字字符之外的所有内容

php - 如何加入产品和产品变体以获得带有产品名称的每个变体

php - 生成每日交易登记