海明分类器 是给字符分类,应用场景有 url短链,可以给任意字符生成一个短链
依赖md5.js
function string10to62(number) {
var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ'.split(''),
radix = chars.length,
qutient = +number,
arr = [];
do {
mod = qutient % radix;
qutient = (qutient - mod) / radix;
arr.unshift(chars[mod]);
} while (qutient);
return arr.join('');
}
function string62to10(number_code) {
var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ',
radix = chars.length,
number_code = String(number_code),
len = number_code.length,
i = 0,
origin_number = 0;
while (i < len) {
origin_number += Math.pow(radix, i++) * chars.indexOf(number_code.charAt(len - i) || 0);
}
return origin_number;
}
function string10to63(number) {
var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ,'.split(''),
radix = chars.length,
qutient = +number,
arr = [];
do {
mod = qutient % radix;
qutient = (qutient - mod) / radix;
arr.unshift(chars[mod]);
} while (qutient);
return arr.join('');
}
function string63to10(number_code) {
var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ,',
radix = chars.length,
number_code = String(number_code),
len = number_code.length,
i = 0,
origin_number = 0;
while (i < len) {
origin_number += Math.pow(radix, i++) * chars.indexOf(number_code.charAt(len - i) || 0);
}
return origin_number;
}
//海明分类器
function HammingTag(s) {
var s1=hex_md5(s)
var rs='552cb8e71eea229fd1ea8ac0010057e6';//作者的md5
let n='';
for(var i=0;i<s1.length;i++){
n=n+(s1[i]===rs[i]?'0':'1');
}
return parseInt(n,2);
}
//demo
const tag=HammingTag(1);
const id=string63to10(string10to62(tag)+',3')
const key=string10to62(id);
console.log(id)
console.log(key)
const nid=string62to10(key);
const arr=string10to63(nid).split(',').map(function (str) {
return string62to10(str)
})
console.log(arr)