I haven’t posted any code on here in a while, so here’s a quick little bit of code that might be useful to somebody. I had to write some code this week to do conversion from base 10 to base 36 and back, in Dynamics AX. So I just took some simple code from the Wikipedia article on base 36 and converted it to X++. Nothing fancy. The code below is all in a job, but I’ll probably put it in a utility class when I actually implement it.
// https://gist.github.com/andyhuey/5c2404b65939b5fccab8 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; }