Skip to content

Instantly share code, notes, and snippets.

@dtjm
Created October 5, 2012 16:42
Show Gist options
  • Save dtjm/3840909 to your computer and use it in GitHub Desktop.
Save dtjm/3840909 to your computer and use it in GitHub Desktop.
Convert text to base 2-65536 using Unicode characters
(function(){
var toBaseN = function(n, base) {
if(base < 2 || base > 65535) {
throw "Base must be between 2 and 65535 inclusive"
}
var str = "";
var sign = "";
if(n < 0) {
n *= -1;
sign = "-";
}
if(n === 0) {
str = String.fromCharCode(n);
}
while (n !== 0) {
var mod = n % base;
n = Math.floor(n/base);
str = String.fromCharCode(mod) + str;
}
return sign + str;
}
window.toBaseN = toBaseN;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment