Created
September 19, 2014 17:28
-
-
Save panta82/65a351b75faafc32a1e8 to your computer and use it in GitHub Desktop.
decimalToCustom, useful to shorten database ID-s into strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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