Safe Char number encoding / decoding

node v8.16.1
version: 3.0.0
endpointsharetweet
const chars = 'abcdehlkmrtwxyz3469'.toUpperCase() // const i = 19880108151231 const i = 1002558 console.log(i) const to = (decimal) => { let out = '' while (true) { let remainder = (decimal - 1) % chars.length out = chars[remainder] + out; decimal = Math.floor((decimal - 1) / chars.length); if (decimal === 0) break } return out; } let t = to(i) console.log(i + ' (' + to(i % 13) + ') ' + t) console.log('eg. \'' + i + '.' + to(i % 13) + t + '\'') const from = (alpha) => { const crs = chars.split('') const letters = alpha.split('') let out = 0 for (let i = 0; i < letters.length; i++) { let indexPos = crs.indexOf(letters[letters.length - 1 - i]) out += (indexPos + 1) * Math.pow(crs.length, i) } return out } let f = from(t) console.log(t + ' = ' + f + ' (' + to(f % 13) + ')')
Loading…

no comments

    sign in to comment