Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dieseltravis/9848160 to your computer and use it in GitHub Desktop.
Save dieseltravis/9848160 to your computer and use it in GitHub Desktop.
js custom number base
// 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