Last active
August 29, 2015 13:57
-
-
Save dieseltravis/9848160 to your computer and use it in GitHub Desktop.
js custom number base
This file contains 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
// base 10 integer to custom any base string | |
var toBaseX = function (value, baseChars) { | |
var result = ""; | |
var targetBase = baseChars.length; | |
do { | |
result = baseChars[value % targetBase] + result; | |
value = Math.floor(value / targetBase); | |
} while (value); | |
return result; | |
}; | |
// example base 66 | |
toBaseX(66, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~-_.".split("")); // "10" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment