在Javascript / jQuery中从字符串生成一个哈希 [英] Generate a Hash from string in Javascript/jQuery
本文介绍了在Javascript / jQuery中从字符串生成一个哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将字符串转换为某种形式的散列。这可能在Javascript / jQuery中吗?
我没有使用服务器端语言,所以我不能这样做。
$ b $if(this.length === 0)返回散列;
for(i = 0; i
hash | = 0; //转换为32位整数
}
返回散列;
};
来源:
http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas -string-hashcode-method /
I need to convert strings to some form of hash. Is this possible in Javascript/jQuery?
I'm not utilizing a server-side language so I can't do it that way.
解决方案
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
Source: http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
这篇关于在Javascript / jQuery中从字符串生成一个哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文