Title: Re: value conversion utility
Has anyone written a value conversion (Hex, bit, signed, unsigned, etc. )utility in Revolution?

Hi Kurt,

I have not, and decided not to put conversion routines in my library because I've never used the conversion extensions I wrote in CompileIt!

FWIW, here is CompileIt! source to convert positive base 10 integers to any base from 2 to 35 and vice versa:

function decimalToX theNumber, numberBase
  -- Returns a numberString that uses base numberBase notation to reresent integer theNumber
  put theNumber + 0.0 into realNumber -- CompileIt types theNumber as string...convert to real
  put theNumber + 0 into myNumber -- CompileIt types theNumber as string...convert to integer
  if realNumber - myNumber is not 0 or myNumber < 0 then return "Number not a positive integer"
  put numberBase + 0 into myBase -- CompileIt types numberBase as string...convert to integer
  if myBase < 2 or myBase > 35 then return "Invalid number base"
  put myBase into checkSum
  put 0 into x
  repeat until checkSum > myNumber
    put myBase * checkSum into checkSum
    add 1 to x
  end repeat
  put empty into numberString
  repeat until x = 0
    get myBase^x
    put myNumber div it into theDigit
    put myNumber mod it into myNumber
    if theDigit > -1 and theDigit < 10 then put  numToChar(theDigit + 48) after numberString
    else put numToChar(theDigit + 55) after numberString
    subtract 1 from x
  end repeat
  if myNumber < 10 then put  numToChar(myNumber + 48) after numberString
  else put numToChar(myNumber + 55) after numberString
  return numberString
end decimalToX

function xToDecimal numberString, numberBase  -- could benefit by using handles
  -- Returns the decimal equivalent of a numberString that uses base numberBase notation
  put numberBase + 0 into myBase -- CompileIt types numberBase as string...convert to integer
  if myBase < 2 or myBase > 35 then return "Invalid number base"
  put 0 into theNumber
  put myBase + 55 into tooMany
  repeat with x =  1 to length(numberString)
    put myBase into theDigit
    get charToNum(char x of numberString)
    if it > 47 and it < 58 then put  it - 48 into theDigit   -- "0" to "9"
    else if it > 64 and it < tooMany then put it - 55 into theDigit  -- "A" to upper digit
    if theDigit >= myBase then return "Invalid Digit"
    put theNumber*myBase+theDigit into theNumber
  end repeat
  return theNumber
end xToDecimal
-- 

Rob Cozens
CCW, Serendipity Software Company
http://www.oenolog.com/who.htm

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)



Reply via email to