Created
May 8, 2014 12:45
-
-
Save andyhuey/5c2404b65939b5fccab8 to your computer and use it in GitHub Desktop.
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
static void AjhBase36Test(Args _args) | |
{ | |
// ajh 2014-05-07: 611.23 | |
// adapted from http://en.wikipedia.org/wiki/Base_36. | |
// note: handles non-negative integers only. | |
#define.NBASE(36) | |
#define.CLIST("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
int64 base36_decode(str base36_input) | |
{ | |
int64 result = 0; | |
real pow = 0; | |
int i, pos; | |
str c; | |
for (i = strLen(base36_input); i > 0; i--) | |
{ | |
c = subStr(base36_input, i, 1); | |
pos = strScan(#CLIST, c, 1, #NBASE) - 1; | |
if (pos == -1) | |
return pos; // error | |
result += pos * power(#NBASE, pow); | |
pow++; | |
} | |
return result; | |
} | |
str base36_encode(int64 input_num) | |
{ | |
int64 work_num = input_num; | |
str result = ""; | |
int digitidx; | |
str c; | |
do { | |
digitidx = int642int(work_num mod #NBASE); | |
c = subStr(#CLIST, digitidx + 1, 1); | |
result += c; | |
work_num = work_num div #NBASE; | |
} while (work_num > 0); | |
return strReverse(result); | |
} | |
print base36_decode(""); | |
print base36_decode("0"); | |
print base36_decode("A"); // 10 | |
print base36_decode("7PS"); // 10,000 | |
print base36_decode("255S"); // 100,000 | |
print base36_decode("!@#$"); // error | |
print base36_encode(0); | |
print base36_encode(123); | |
print base36_encode(10000); | |
print base36_encode(100000); | |
pause; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment