Created
October 5, 2012 16:42
-
-
Save dtjm/3840909 to your computer and use it in GitHub Desktop.
Convert text to base 2-65536 using Unicode characters
This file contains hidden or 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(){ | |
| 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