[SNIPPETS] large number base conversion

Shao Sean shaosean at wehostmacs.com
Thu Jun 14 03:28:33 EDT 2007


Perhaps someone is better at math than me :-)
Here are two functions that can be used to convert between bases  
(base-2 to base-36) Why use these instead of the built-in  
"baseConvert" function? Simple - these can handle larger numbers (not  
too certain of the accuracy in really large numbers due to a lack of  
a way to test the results)


function convertFromDecimal pIn, pBase
   local tInLength
   local tNumber
   local tOut

   put the length of pIn into tInLength
   repeat with i = tInLength DOWN TO 1
     put (pIn MOD pBase^i) DIV pBase^(i-1) into tNumber
     put baseConvert(tNumber, 10, pBase) after tOut
   end repeat
   return tOut
end convertFromDecimal


function convertToDecimal pIn, pBase
   local tInLength
   local tNumber
   local tOut

   put the length of pIn into tInLength
   repeat with i = tInLength DOWN TO 0
     if (char tInLength-i of pIn is EMPTY) then next repeat
     put baseConvert(char tInLength-i of pIn, pBase, 10) into tNumber
     put tOut + (tNumber * pBase^i) into tOut
   end repeat
   return tOut
end convertToDecimal



More information about the Use-livecode mailing list