Python实现java或者.net的getHashCode()函数,pythongethashcode,hashCode在.ne
Python实现java或者.net的getHashCode()函数,pythongethashcode,hashCode在.ne
hashCode在.net和java中应用很普遍,但是python没有内置的getHashcode函数。
如下是java版本getHashcode的python实现
# -*- coding:utf-8 -*-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)if __name__ == '__main__': print getHashCode('http://byrx.net/') print getHashCode('http://byrx.net/code-snippet/2311/C-rumenjiaocheng-c-multithreading-process-course') print getHashCode('http://byrx.net/code-snippet/2321/C-rumenjiaocheng-usage-arrow-unsafe-code-block/') print getHashCode('http://byrx.net/code-snippet/2322/mysql-achieve-sql-server-with-lock') print getHashCode('http://byrx.net/') print getHashCode('http://byrx.net/code-snippet/2324/java-unit-test-usage-jMockit-mock-jingtailei')
相关内容
- Python正则表达式入门,python正则表达式,一. 编译正则表
- Python使用elaphe包生成二维码,pythonelaphe,首先需要安装
- Python正则表达式前向否定断言使用示例,python正则表达
- python itertools模块学习笔记,pythonitertools,python itert
- Decorate an output stream with print-like methods,decorateprint-lik
- Python合并两个字典,python合并字典,Python合并两个字典
- python生成文件目录树代码,python生成树代码,code:import
- python实现Memento模式,python实现memento,下面的例子通过
- Python生成字母,数字,特殊符号组成的随机密码,pyt
- Tkinter的ToolTip控件,tkintertooltip,Tkinter的Tool
评论关闭