>Anyone have a script or link? Unfortunately any of the bit operations return
>integers, not the 8-position binary number...

Below is some old code (D6.5) I wrote that converts between various number
systems (see convertBase below). Some of these routines rely on functions
that may be missing, such as "uppercase", "isAlpha", and "isOctalDigit".
You can use these dummy versions or write some real versions:

on uppercase input
  return input -- Just return the same character
end

on isAlpha
  return TRUE -- dummy, doesn't really do error checking
end

on isOctalDigit
  return TRUE -- dummy, doesn't really do error checking
end




-- Print out octal table
on octal startNum, endNum

  if voidP(startNum) then
    set startNum = 1
  end if

  if voidP(endNum)   then
    set endNum   = 512
  end if

  set startNum = clipValue (startNum, 1, 512)
  set endNum   = clipValue (endNum,   1, 512)

  if endNum < startNum then
    set temp = startNum
    set startNum = endNum
    set endNum = temp
  end if

  repeat with n = startNum to endNum
    put decimalToOctal (n) && ":" && n
  end repeat

end octal


on clipValue checkVal, minVal, maxVal
  set temp = checkVal
  set temp = max (temp, minVal)
  set temp = min (temp, maxVal)
  return temp
end clipValue

-- Print out decimal table based on specified octals
on inputOctal startOctal, endOctal

  if voidP(startOctal) then
    set startNum = 1
  else
    set startNum = octalToDecimal (startOctal)
  end if

  if voidP(endOctal)   then
    set endNum   = 512
  else
    set endNum = octalToDecimal (endOctal)
    if endNum = 0 then
      set endNum = 512
    end if
  end if

  set startNum = clipValue (startNum, 1, 512)
  set endNum   = clipValue (endNum,   1, 512)

  if endNum < startNum then
    set temp = startNum
    set startNum = endNum
    set endNum = temp
  end if

  repeat with n = startNum to endNum
    put  n && ":" && decimalToOctal (n)
  end repeat

end inputOctal




on octalToDecimal alphaNum
  -- Translate a number in "A11" format into a decimal

  set invalid = FALSE
  if length (alphaNum) <> 3 then
    set invalid = TRUE
  end if
  -- Really no need to check this as higher characters are allowed
  if not isAlpha (char 1 of alphaNum) then
    set invalid = TRUE
  end if

  if not isOctalDigit (char 2 of alphaNum) then
    set invalid = TRUE
  end if
  if not isOctalDigit (char 3 of alphaNum) then
    set invalid = TRUE
  end if

  if invalid = TRUE then
    put alphaNum && "is not a valid A11 format"
  end if

  if not invalid then
    set decimal = (charToNum (uppercase(char 1 of alphaNum)) - charToNum
("A")) * 64
    set decimal = decimal + 8 * (value (char 2 of alphaNum) - 1)
    set decimal = decimal +      value (char 3 of alphaNum)
  else
    set decimal = 0
  end if

  return decimal
end octalToDecimal


on decimalToOctal castNo
  -- FIX - used to only go to "H88" now it goes beyond that
  -- Translate a decimal number into "A11" format
  if castNo <= 32 * 1024 then

    set letter = integer( (castNo - 1) / 64) + 1

    set row    = integer( (castNo - ((letter - 1) * 64)) / 8)      + 1

    set column =           castNo - ((letter - 1) * 64) - ((row-1) * 8)
    if column =  0  then
      set row = row - 1
      set column = 8
    end if
    set castAlpha = (numToChar (charToNum ("A") + letter-1)) & string(row)
& string(column)

  else
    set castAlpha = EMPTY
  end if

  return castAlpha
end decimalToOctal


on binaryTest
  repeat with x = 0 to 31
    put "Decimal:" && x & ", Binary:" && convertBase (x, 10, 2)
  end repeat
end

on hexTest
  repeat with x = 0 to 255
    put "Decimal:" && x & ", Hex:" && convertBase (x, 10, 16)
  end repeat
end hexTest

on decimalToHex decimal
  return convertBase (decimal, 10, 16)
end decimalToHex

on hexToDecimal hex
  return convertBase (hex, 16, 10)
end hexToDecimal

on convertBase inNum, oldBase, newBase
  set digits = ["0", "1", "2", "3", "4", "5", "6", "7", �
                "8", "9", "A", "B", "C", "D", "E", "F"]

  -- No need to perform conversion
  if newBase = oldBase then return inNum

  set inputString = string (inNum)
  set strLength   = length (inputString)

  if oldBase = 10 then
    -- Ensure that input is an integer, if using base 10
    if not integerP (inNum)  then
      alert "Input is not a decimal number"
      return VOID
    end if
    set base10sum = value (inNum)
    if base10Sum < 0 then
      alert "Absolute value will be used"
      set base10Sum = abs (base10sum)
    end if

  else
    -- Convert the input number to base 10
    set base10sum = 0

    -- Convert each digit in turn
    repeat with x = strLength down to 1
      set thisChar  = char x of inputString
      set thisASCII = charToNum(thisChar)
      -- Convert lower case letters to upper case
      if thisASCII >= 97 and thisASCII <= 122 then
        set thisChar = numToChar (thisASCII - 32)
      end if
      set thisDigit =  getOne (digits, thisChar) - 1
      if thisDigit >= oldBase or thisDigit < 0 then
        alert "invalid digit"
        return VOID
      end if

      set base10sum = base10sum + integer (power (oldBase, strLength - x) *
thisDigit)


    end repeat
  end if

  -- We've converted it to base 10
  if newBase = 10 then
    return integer(base10sum)
  else

    -- Convert it from, say, decimal to hex, if necessary
    set working = base10sum
    set outNum = EMPTY

    repeat while TRUE
      set remainder = working mod newBase
      put getAt (digits, remainder+1) before outNum
      if working < newBase then
        exit repeat
      end if
      set working = working / newBase
    end repeat

    -- Non-decimal values returned as a string
    return outNum
  end if

end convertBase


Cheers,
Bruce

---------
Looking for an ActionScript book as good as Lingo and Director in a
Nutshell? Check out "ActionScript: The Definitive Guide" from O'Reilly.
Written by Colin Moock, edited by Bruce Epstein. Available Now!
http://www.amazon.com/exec/obidos/ASIN/1565928520/zeusproductions
--------



[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/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