Skip to content

Instantly share code, notes, and snippets.

@joshje
Created September 20, 2011 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshje/1229568 to your computer and use it in GitHub Desktop.
Save joshje/1229568 to your computer and use it in GitHub Desktop.
Base60 Converter
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Base60 Converter</title>
</head>
<body>
<p><label for="n">Number</label><input id="n"> &rarr; <label for="s">Base 60</label><input id="s"></p>
<script>
ni = document.getElementById('n');
si = document.getElementById('s');
n.onkeyup = function() {
si.value = num_to_sxg(this.value);
}
s.onkeyup = function() {
ni.value = sxg_to_num(this.value);
}
function num_to_sxg(n) {
s = "";
m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz";
if (n===undefined || n===0) { return 0; }
while (n>0) {
d = n % 60;
s = m[d]+s;
n = (n-d)/60;
}
return s
}
function sxg_to_num(s) {
n = 0;
j = s.length;
for (i=0;i<j;i++) {
c = s.charCodeAt(i);
if (c>=48 && c<=57) { c=c-48; }
else if (c>=65 && c<=72) { c-=55; }
else if (c==73 || c==108) { c=1; }
else if (c>=74 && c<=78) { c-=56; }
else if (c==79) { c=0; }
else if (c>=80 && c<=90) { c-=57; }
else if (c==95) { c=34; }
else if (c>=97 && c<=107) { c-=62; }
else if (c>=109 && c<=122) { c-=63; }
else { c = 0; }
n = 60*n + c;
}
return n;
}
</script>
</body>
</html>
@joshje
Copy link
Author

joshje commented Sep 20, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment