Skip to content

Instantly share code, notes, and snippets.

@ileathan
Last active January 29, 2018 00:39
Show Gist options
  • Save ileathan/61a51d74b2082bed310065a7b324bcd9 to your computer and use it in GitHub Desktop.
Save ileathan/61a51d74b2082bed310065a7b324bcd9 to your computer and use it in GitHub Desktop.
Continually mod and map.
encode = (n, a) => {
var r = [];
do {
n = (n - (r[r.length] = n % a.length)) / a.length
} while(n);
return r.map(n=>a[n]).reverse().join('')
};
encode(4095, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')
// Outputs "//"
encode(4095, '0123456789abcdef')
// Outputs "ffff"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment