-
-
Save sebastien-p/1170594 to your computer and use it in GitHub Desktop.
base62 decode
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 ( | |
a, // base62 encoded string | |
b, // placeholder for result | |
c, // placeholder for iterator | |
d // placeholder for char code | |
) { | |
for ( | |
b = c = ( // 'false - 1' will return '-1' and 'true - 1' will return '0' | |
a === (/\W|_|^$/.test(a += "") || a) // tests if 'a' is a properly base62-encoded string and coerces it to one | |
) - 1; // so, 'b' and 'c' are initialized with either '-1' or '0' | |
d = a.charCodeAt(c++); // if 'c' equals '-1', 'd' is 'NaN' which breaks the loop execution | |
) | |
b = b * 62 + d - [, 48, 29, 87][d >> 5]; // See comments : https://gist.github.com/1170594#gistcomment-48129 | |
return b // positive base10 integer or '-1' if 'a' is not a base62 encoded string | |
} |
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(a,b,c,d){for(b=c=(a===(/\W|_|^$/.test(a+="")||a))-1;d=a.charCodeAt(c++);)b=b*62+d-[,48,29,87][d>>5];return b} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Sebastien P. https://twitter.com/#!/_sebastienp | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "base62 decode", | |
"description": "A JavaScript implementation of base62 decode.", | |
"keywords": [ | |
"base62", | |
"decode", | |
"string", | |
"number", | |
"integer" | |
] | |
} |
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
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>3748986303764</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var base62decode = function(a,b,c,d){for(b=c=(a===(/\W|_|^$/.test(a+="")||a))-1;d=a.charCodeAt(c++);)b=b*62+d-[,48,29,87][d>>5];return b}; | |
document.getElementById("ret").innerHTML = base62decode("140bytes") | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found this useful and converted it into C# if anyone ever needs it. Not intended to be 140 bytes small.