Skip to content

Instantly share code, notes, and snippets.

@vivekseth
Created October 8, 2014 16:51
Show Gist options
  • Save vivekseth/bddc2a1587e760d01805 to your computer and use it in GitHub Desktop.
Save vivekseth/bddc2a1587e760d01805 to your computer and use it in GitHub Desktop.
function assert(cond) {
if (cond) {
throw "Assertion Error";
}
}
function encodeArbitraryBase(value, base) {
assert(typeof value == "number");
assert(typeof base == "number");
assert(value === parseInt(value))
assert(2 <= base);
assert(base <= 96);
var offset = 32;
var str = "";
var digitArr = [];
if (value == 0) {
digitArr.push(0);
} else {
while (value > 0) {
var rem = value % base;
digitArr.push(rem);
value = Math.floor(value / base);
}
}
for(var i=digitArr.length; i>0; i--) {
str += String.fromCharCode(digitArr[i-1] + offset);
}
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment