复制python和node.js中java.lang.String.hashCode()的输出的函数

bal*_*bin 5 javascript python hashcode redis node.js

我正在尝试实现一个函数来生成等效于node.js和python的java hashCode,以实现Redis分片。我正在关注下面提到的非常好的博客@链接,以实现此 http://mechanics.flite.com/blog/2013/06/27/sharding-redis/

但是如果字符串包含一些不像下面的示例那样的ascii字符,我会陷入hashCode的差异。对于常规字符串,我可以同时将node.js和python给我相同的哈希码。

这是我用来生成此代码:

- 蟒蛇

def _java_hashcode(s):
    hash_code = 0
    for char in s:
        hash_code = 31*h + ord(char)

    return ctypes.c_int32(h).value   
Run Code Online (Sandbox Code Playgroud)

-根据上述博客的节点

String.prototype.hashCode = function() {
  for(var ret = 0, i = 0, len = this.length; i < len; i++) {
    ret = (31 * ret + this.charCodeAt(i)) << 0;
  }
  return ret;
};
Run Code Online (Sandbox Code Playgroud)

--Python输出

For string '?:s??2?*?=x?' hash is = 2014651066
For string '359196048149234' hash is = 1145341990
Run Code Online (Sandbox Code Playgroud)

-节点输出

For string '?:s??2?*?=x?' hash is = 150370768
For string '359196048149234' hash is = 1145341990
Run Code Online (Sandbox Code Playgroud)

请指导我,我在哪里..我需要在python和node程序中设置某种编码类型吗,我尝试了一些,但是我的程序在python中中断了。

小智 5

def java_string_hashcode(s):
    """Mimic Java's hashCode in python 2"""
    try:
        s = unicode(s)
    except:
        try:
            s = unicode(s.decode('utf8'))
        except:
            raise Exception("Please enter a unicode type string or utf8 bytestring.")
    h = 0
    for c in s:
        h = int((((31 * h + ord(c)) ^ 0x80000000) & 0xFFFFFFFF) - 0x80000000)
    return h
Run Code Online (Sandbox Code Playgroud)

这是您应该在python 2中执行

的方法。问题有两个:

  • 您应该使用unicode类型,并确保是这样。
  • 每一步之后,您都需要通过使用按位操作为后续步骤获取正确的int类型来防止python自动转换为long类型。(交换符号位,将其掩码为32位,然后减去符号位的数量将为我们提供负整数(如果存在符号位)和正整数(如果不存在符号位)。这类似于Java中的int行为。 。

另外,与其他答案一样,对于硬编码的非ASCII字符,请将您的源文件另存为utf8,并在文件顶部写入:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)

并确保是否收到用户输入,将其作为Unicode类型而不是字符串类型进行处理。(对于python 3来说不是问题)


小智 0

除非另有说明,Python 2 将采用 ASCII 编码。从PEP 0263开始,您可以在文件顶部使用以下内容指定 utf-8 编码的字符串。

#!/usr/bin/python
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)