下面的哈希函数与标准的哈希有些不一样,有需要的可以拿去用
C版本的hash
javaScript的Hash
hashCode: function(str) {
//将字符串转成16进制再去与
var bin_code = new Uint32Array();
var offset = new Uint8Array();
let k = strToHexCharCode(str[0]);
offset = k & 0x3F;
offset = offset % (str.length - 5);
bin_code = (strToHexCharCode(str[offset]) & 0x7f) << 24
| (strToHexCharCode(str[offset + 1]) & 0xff) << 16
| (strToHexCharCode(str[offset + 2]) & 0xff) << 8
| (strToHexCharCode(str[offset + 3]) & 0xff);
let sss = Math.pow(10, 6);
bin_code = bin_code % sss;
return bin_code;
},
//字符串转十六进制
function strToHexCharCode(str) {
if (str === "")
return "";
var hexCharCode = [];
hexCharCode.push("0x");
for (var i = 0; i < str.length; i++) {
hexCharCode.push((str.charCodeAt(i)).toString(16));
}
return hexCharCode.join("");
}