-- convert number into a string representation in the given base
on toString from aNumber by aRadix
if aRadix < 2 or aRadix > 36 then
error "unsupported radix " + (aRadix as text)
end if
set resultString to ""
repeat while aNumber > 0
set digit to (aNumber mod aRadix)
if digit > 10 then
set digit to character id (digit - 10 + (id of "A"))
end
set resultString to (digit as text) & resultString
set aNumber to aNumber div aRadix as integer
end repeat
return resultString
end
-- parse a string representing a number in the given base
on toNumber from aString by aRadix
if aRadix < 2 or aRadix > 36 then
error "unsupported radix " + (aRadix as text)
end if
set resultNum to 0
repeat with aDigit in characters of aString
set charCode to id of aDigit
if charCode >= id of "0" and charCode <= id of "9" then
set digitValue to aDigit as integer
else if charCode >= id of "a" and charCode <= id of "z" then
set digitValue to charCode - (id of "a") + 10
else if charCode >= id of "A" and charCode <= id of "Z" then
set digitValue to charCode - (id of "A") + 10
end
if digitValue >= aRadix then
error "Digit '" + aDigit + "' out of range for base " + aRadix
end
set resultNum to resultNum * aRadix + digitValue
end
return resultNum
end
toString from 1318544445 by 36
toNumber from "LT0ZEL" by 36