Created
October 8, 2014 16:51
-
-
Save vivekseth/bddc2a1587e760d01805 to your computer and use it in GitHub Desktop.
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
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