16 bit numbers?

Cubist at aol.com Cubist at aol.com
Sat Jun 26 16:16:20 EDT 2004


   All this stuff about binaryEncode and unicode and so on is all very well 
and good, but nobody seems to have actually addressed the *general* problem of 
converting numbers from one base to another. Here are two functions which do 
just that, converting from [base 10] to [any base from 2 to 36], and vice 
versa. These functions deal with negative numbers and non-integers; if you know 
that you're never going to have to worry about anything besides positive 
integers, it should be fairly easy to nuke the parts that deal with everything else. 
Oh, and if you're in one of those weird countries that uses a comma for a 
decimal point, you'll want to make the obvious change in the code.
   Hope this helps...

function Base2Dec DaNum, DaBase
  put the value of DaNum into DaNum
  put the value of DaBase into DaBase
  if (DaBase is empty) or (DaBase is not an integer) or (DaBase < 2) or 
(DaBase > 36) then return "NA bad base"
  put char 1 to DaBase of "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" into Digitz
  
  -- deal with negatives
  put (char 1 of Danum is "-") into ItzNegative
  if ItzNegative then delete char 1 of Danum
  
  -- deal with fractionals
  if "." is in DaNum then -- there's at least one decimal point
    put offset (".",DaNum) into PtLoc
    put char 1 to (PtLoc - 1) of DaNum into IntPart
    delete char 1 to PtLoc of DaNum
    if  offset (".",DaNum) > 0 then return "NA not a number"
    put DaNum into FracPart
  else -- it's an integer
    put DaNum into IntPart
    put "" into FracPart
  end if
  
  -- detect illegal "digits"
  repeat with K1 = 1 to the length of IntPart
    if char K1 of IntPart is not in Digitz then return ("NA bad digit" && 
DaBase)
  end repeat
  if FracPart is not empty then
    repeat with K1 = 1 to the length of IntPart
      if char K1 of IntPart is not in Digitz then return ("NA bad digit" && 
DaBase)
    end repeat
  end if
  
  -- convert the number from base DaBase to base 10
  put 0 into DaRezult
  repeat (the length of IntPart)
    multiply DaRezult by DaBase
    put char 1 of IntPart into DisDigit
    delete char 1 of IntPart
    add (offset (DisDigit, Digitz) - 1) to DaRezult
  end repeat
  if FracPart is not empty then
    put 1 into DaFrac
    put 1/DaBase into DaKonst
    repeat (the length of FracPart)
      multiply DaFrac by DaKonst
      add (DaFrac * (offset (char 1 of FracPart, Digitz) - 1)) to DaRezult
      delete char 1 of FracPart
    end repeat
  end if
  if ItzNegative then put "-" before DaRezult
  return DaRezult
end Base2Dec

function Dec2Base DaNum, DaBase
  if (DaBase is empty) or (DaBase is not an integer) or (DaBase < 2) or 
(DaBase > 36) then return "NA bad base"
  put char 1 to DaBase of "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" into Digitz
  
  -- deal with negatives
  put (char 1 of Danum is "-") into ItzNegative
  if ItzNegative then delete char 1 of Danum
  
  -- deal with fractionals
  if "." is in DaNum then -- there's at least one decimal point
    put offset (".",DaNum) into PtLoc
    put char 1 to (PtLoc - 1) of DaNum into IntPart
    delete char 1 to PtLoc of DaNum
    if  offset (".",DaNum) > 0 then return "NA not a number"
    put DaNum into FracPart
  else -- it's an integer
    put DaNum into IntPart
    put "" into FracPart
  end if
  
  -- detect illegal "digits"
  if DaNum is not a number then return ("NA not base 10")
  
  -- convert the number from base 10 to base DaBase
  put "" into DaRezult
  repeat
    put char ((IntPart mod DaBase) + 1) of Digitz before DaRezult
    put IntPart div DaBase into IntPart
    if IntPart = 0 then exit repeat
  end repeat
  if FracPart is not empty then
    put "." after DaRezult
    put "." before FracPart
    put 0 into Kounter
    repeat
      add 1 to Kounter
      multiply FracPart by DaBase
      put char (trunc (FracPart) + 1) of Digitz after DaRezult
      subtract trunc (FracPart) from FracPart
      if FracPart = 0 or Kounter = 15 then exit repeat
    end repeat
  end if
  if ItzNegative 


More information about the use-livecode mailing list