[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I've been trying to decipher some formulae I found online which
> convert hex to decimal numbers, with a view to re-writing it in
> lingo, but my head is hurting already.


Hi Chris,

You'll find below the fastest 32-bit conversion handlers that I am know of.
The hex() handler is much slower with negative numbers than with positive,
and it returns "" for 0, so there is still room for improvement there.

Cheers,

James



----------------------------------------------------------------------------

on hexToDecimal(hex) ------------------------------------------------
  -- Returns the integer value corresponding to any 8-digit
  -- hexadecimal string.  Functions correctly with negative numbers
  -- (> 7FFFFFFF).  Expects the <hex> string to contain 8 characters
  -- or less, in the range "0123456789ABCDEF"... Or you can uncomment
  -- the error checking lines below, in which case a self-explanatory
  -- error symbol will be returned instead of an (invalid) integer.
  -- Inspired by code by Andrew White.
  -------------------------------------------------------------------

  -- -- Error checking
  -- hex = cleanString(hex)
  -- if not stringP(hex) then
  --   return hex -- #integerOverflow | #invalidHexString
  -- end if
  -- -- End of error checking
  
  counter = hex.char.count
  decimal = 0
  
  repeat with i = 1 to counter
    decimal = (decimal*16) + (offset(hex.char[i], "123456789ABCDEF"))
  end repeat
  
  return decimal
end hexToDecimal



on cleanString(hex)
  -- Strips the string <hex> of all spaces, initial "#" or "0"
  -- characters, and checks that the figures are all in the range
  -- "0123456789ABCDEF".  Returns the cleaned <hex> string, or a
  -- self-explanatory error symbol.
  
  if not stringP(hex) then
    return #invalidString
  end if
  
  repeat while TRUE
    set spacePlace = offset (" ", hex)
    if spacePlace then
      delete char spacePlace of hex
    else
      exit repeat
    end if
  end repeat
  
  repeat while TRUE
    case hex.char[1] of
      "0", "#":
        delete hex.char[1]
      otherwise
        exit repeat
    end case
  end repeat
  
  i = hex.char.count
  repeat while i
    if not offset(hex.char[i], "0123456789ABCDEF") then
      return #invalidHexString
    end if
    i = i - 1
  end repeat
  
  if i > 8 then
    return #integerOverflow
  else
    
    return hex
  end if
end cleanString



on hex(aDecimal)
  -- Returns the hexadecimal string corresponding to any 4-byte
  -- integer, including negative numbers.  If you uncomment the
  -- first three lines of code and <aDecimal> is not an
  -- integer a self-explanatory error symbol is returned.
  -- NOTE: returns "" for 0
  -------------------------------------------------------------------
  
  -- -- Error checking
  -- if not integerP(aDecimal) then
  --   return #invalidInteger
  -- end if
  -- -- End of error checking
  
  if aDecimal > 0 then
    -- Use recursion if the number is positive
    hexDigit = aDecimal mod 16
    if hexDigit < 10 then
      return hex(aDecimal / 16) & hexDigit
    else
      return hex(aDecimal / 16) & ("ABCDEF").char[hexDigit - 9]
    end if
    
  else if aDecimal then -- The number is negative
    -- Convert the number's positive counterpart, ...
    inverse = hex(aDecimal + the maxInteger + 1)
    -- ... pad with zeros if necessary, ...
    i = 8 - inverse.char.count
    
    if i then
      i = i - 1
      repeat while i
        put "0" before inverse
        i = i - 1
      end repeat
      -- ... then convert back to a negative number
      put 8 before char 1 of inverse
    else
      char1 = ("9ABCDEF").char[value(inverse.char[1])]
      put char1 into char 1 of inverse
    end if
    return inverse
    
  else -- aDecimal is 0
    return ""
  end if
end hex

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
[EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to