Decoding Base 36 numbers

Decoding Base 36 numbers

The following function is a DELPHI (Pascal) routine to convert a base 36 number to a string. (C equivalent routine here)

function IDtoStr(ID : Cardinal) : string;
const
  Lookup = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  BASE = 36;
begin
  result := '';
  repeat
    result := Lookup[ID mod BASE +1] + result;
    ID := ID div BASE;
  until ID=0;
end;

This routine can easily be generalised to decode ANY base number, simply by changing the reference to the number ‘36’, and extending the lookup array (beyond base 36 character symbols become less meaningful).