Skip to content

Instantly share code, notes, and snippets.

@panta82
Created September 19, 2014 17:28
Show Gist options
  • Save panta82/65a351b75faafc32a1e8 to your computer and use it in GitHub Desktop.
Save panta82/65a351b75faafc32a1e8 to your computer and use it in GitHub Desktop.
decimalToCustom, useful to shorten database ID-s into strings
function decimalToCustom(decimal, alphabet) {
var alphabetSize = alphabet.length,
remainder,
result = [];
while (decimal > 0) {
remainder = decimal % alphabetSize,
decimal = Math.floor(decimal / alphabetSize);
result.unshift(alphabet[remainder]);
}
return result.join('');
}
var hexes = "0123456789ABCDEF"
console.log(decimalToCustom(153530, hexes));
var letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i < 10000; i++) {
console.log(decimalToCustom(i, letters));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment