Created
June 10, 2013 14:32
-
-
Save meeDamian/5749143 to your computer and use it in GitHub Desktop.
Convert number to it's base-64 representation, and reverse it. It is NOT THE SAME as Base64 encoding!
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
var Base64 = function(){}; | |
Base64._rixits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_"; | |
Base64.toHash = function( n ) { | |
if( isNaN(Number(n)) || n===null || n===Number.POSITIVE_INFINITY || n<0 ) throw "The input is not valid"; | |
n = Math.floor( n ); | |
var result = ''; | |
do result = this._rixits.charAt(n%64) + result; | |
while( n=Math.floor(n/64) ); | |
return result; // String | |
}; | |
Base64.toNumber = function( h ) { | |
for(var i=0, result=0; i<h.length; i++) result = (result*64) + this._rixits.indexOf(h.charAt(i)); | |
return result; // Integer | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment