<!doctype html public "-//W3C//DTD W3 HTML//EN">
<html><head><style type="text/css"><!--
blockquote, dl, ul, ol, li { padding-top: 0 ; padding-bottom: 0 }
 --></style><title>Re: value conversion utility</title></head><body>
<blockquote type="cite" cite>Has anyone written a value conversion
(Hex, bit, signed, unsigned, etc. )utility in Revolution?</blockquote>
<div><br></div>
<div>Hi Kurt,</div>
<div><br></div>
<div>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!</div>
<div><br></div>
<div>FWIW, here is CompileIt! source to convert positive base 10
integers to any base from 2 to 35 and vice versa:</div>
<div><br></div>
<div><font face="Geneva" color="#000000">function decimalToX
theNumber, numberBase<br>
  -- Returns a numberString that uses base numberBase notation to
reresent integer theNumber<br>
  put theNumber + 0.0 into realNumber -- CompileIt types
theNumber as string...convert to real<br>
  put theNumber + 0 into myNumber -- CompileIt types theNumber as
string...convert to integer<br>
  if realNumber - myNumber is not 0 or myNumber < 0 then
return "Number not a positive integer"<br>
  put numberBase + 0 into myBase -- CompileIt types numberBase as
string...convert to integer<br>
  if myBase < 2 or myBase > 35 then return "Invalid
number base"<br>
  put myBase into checkSum<br>
  put 0 into x<br>
  repeat until checkSum > myNumber<br>
    put myBase * checkSum into checkSum<br>
    add 1 to x<br>
  end repeat<br>
  put empty into numberString<br>
  repeat until x = 0<br>
    get myBase^x<br>
    put myNumber div it into theDigit<br>
    put myNumber mod it into myNumber<br>
    if theDigit > -1 and theDigit < 10 then put 
numToChar(theDigit + 48) after numberString<br>
    else put numToChar(theDigit + 55) after
numberString<br>
    subtract 1 from x<br>
  end repeat<br>
  if myNumber < 10 then put  numToChar(myNumber + 48)
after numberString<br>
  else put numToChar(myNumber + 55) after numberString<br>
  return numberString<br>
end decimalToX</font></div>
<div><br></div>
<div><font face="Geneva" color="#000000">function xToDecimal
numberString, numberBase  -- could benefit by using handles<br>
  -- Returns the decimal equivalent of a numberString that uses
base numberBase notation<br>
  put numberBase + 0 into myBase -- CompileIt types numberBase as
string...convert to integer<br>
  if myBase < 2 or myBase > 35 then return "Invalid
number base"<br>
  put 0 into theNumber<br>
  put myBase + 55 into tooMany<br>
  repeat with x =  1 to length(numberString)<br>
    put myBase into theDigit<br>
    get charToNum(char x of numberString)<br>
    if it > 47 and it < 58 then put  it - 48
into theDigit   -- "0" to "9"<br>
    else if it > 64 and it < tooMany then put it
- 55 into theDigit  -- "A" to upper digit<br>
    if theDigit >= myBase then return "Invalid
Digit"<br>
    put theNumber*myBase+theDigit into theNumber<br>
  end repeat<br>
  return theNumber<br>
end xToDecimal</font></div>
<x-sigsep><pre>-- 
</pre></x-sigsep>
<div><br>
Rob Cozens<br>
CCW, Serendipity Software Company<br>
http://www.oenolog.com/who.htm<br>
<br>
"And I, which was two fooles, do so grow three;<br>
Who are a little wise, the best fooles bee."<br>
<br>
from "The Triple Foole" by John Donne (1572-1631)</div>
</body>
</html>