Skip to content

Instantly share code, notes, and snippets.

@meeDamian
Created June 10, 2013 14:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meeDamian/5749143 to your computer and use it in GitHub Desktop.
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!
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