Created
December 21, 2011 05:04
-
-
Save alexkirmse/1504665 to your computer and use it in GitHub Desktop.
Bijective encoding function in JavaScript for Base62
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
// Simple port of a ruby bijective encode | |
// Ported from this Ruby example https://gist.github.com/1073996 | |
function bijectiveEncode(i){ | |
var alphaNumArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(''); | |
if (i == 0){ | |
return alphaNumArray[0]; | |
} | |
s = []; | |
var base = alphaNumArray.length; | |
while (i > 0){ | |
console.log(i); | |
s.push(alphaNumArray[i % base]); | |
i = Math.floor(i / base); | |
} | |
var enc = s.reverse(); | |
return enc.join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for posting. I found this, then I found this link that has the encode and decode together. https://github.com/delight-im/ShortURL/blob/master/JavaScript/ShortURL.js