5

Is it possible in Javascript to convert something like this: d131dd02c5e6eec4 693d9a0698aff95c 2fcab58712467eab 4004583eb8fb7f89, which is the result of an MD5 hash function, into an integer ?

4

4 回答 4

6

可能并不完美,但这适合我的需要

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;
}
于 2018-06-13T23:30:24.607 回答
2

这看起来像一个十六进制数,因此您可以尝试使用该parseInt函数并传入十六进制数:

var num = parseInt(string, 16);

编辑:此方法实际上不起作用。有关详细信息,请参阅评论。

于 2013-06-20T13:44:11.767 回答
1

也许这个https://github.com/lovell/farmhash

const farmhash = require('farmhash');
const hexDigest = crypto.createHash('md5').update().digest('hex');
farmhash.fingerprint64(hexDigest);
于 2018-01-02T13:11:58.333 回答
0

ES6 版本,字符串到 0-9 之间的整数:

string.split('').map(i => i.charCodeAt(0)).reduce((a, b) => a + b, 0) % 10
于 2020-03-19T12:02:28.137 回答