在Javascript中是否可以将这样的内容:d131dd02c5e6eec4 693d9a0698aff95c 2fcab58712467eab 4004583eb8fb7f89
(这是MD5哈希函数的结果)转换为整数?
答案 0 :(得分:3)
答案 1 :(得分:1)
可能不完美,但这符合我的需要
export function stringToIntHash(str, upperbound, lowerbound) {
let result = 0;
for (let i = 0; i < str.length; i++) {
result = result + str.charCodeAt(i);
}
if (!lowerbound) lowerbound = 0;
if (!upperbound) upperbound = 500;
return (result % (upperbound - lowerbound)) + lowerbound;
}
答案 2 :(得分:0)
也许这一个https://github.com/lovell/farmhash?
const farmhash = require('farmhash');
const hexDigest = crypto.createHash('md5').update().digest('hex');
farmhash.fingerprint64(hexDigest);
答案 3 :(得分:0)
ES6版本,字符串到0-9之间的整数:
string.split('').map(i => i.charCodeAt(0)).reduce((a, b) => a + b, 0) % 10