<!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>
&nbsp; -- Returns a numberString that uses base numberBase notation to
reresent integer theNumber<br>
&nbsp; put theNumber + 0.0 into realNumber -- CompileIt types
theNumber as string...convert to real<br>
&nbsp; put theNumber + 0 into myNumber -- CompileIt types theNumber as
string...convert to integer<br>
&nbsp; if realNumber - myNumber is not 0 or myNumber &lt; 0 then
return &quot;Number not a positive integer&quot;<br>
&nbsp; put numberBase + 0 into myBase -- CompileIt types numberBase as
string...convert to integer<br>
&nbsp; if myBase &lt; 2 or myBase &gt; 35 then return &quot;Invalid
number base&quot;<br>
&nbsp; put myBase into checkSum<br>
&nbsp; put 0 into x<br>
&nbsp; repeat until checkSum &gt; myNumber<br>
&nbsp;&nbsp;&nbsp; put myBase * checkSum into checkSum<br>
&nbsp;&nbsp;&nbsp; add 1 to x<br>
&nbsp; end repeat<br>
&nbsp; put empty into numberString<br>
&nbsp; repeat until x = 0<br>
&nbsp;&nbsp;&nbsp; get myBase^x<br>
&nbsp;&nbsp;&nbsp; put myNumber div it into theDigit<br>
&nbsp;&nbsp;&nbsp; put myNumber mod it into myNumber<br>
&nbsp;&nbsp;&nbsp; if theDigit &gt; -1 and theDigit &lt; 10 then put&nbsp;
numToChar(theDigit + 48) after numberString<br>
&nbsp;&nbsp;&nbsp; else put numToChar(theDigit + 55) after
numberString<br>
&nbsp;&nbsp;&nbsp; subtract 1 from x<br>
&nbsp; end repeat<br>
&nbsp; if myNumber &lt; 10 then put&nbsp; numToChar(myNumber + 48)
after numberString<br>
&nbsp; else put numToChar(myNumber + 55) after numberString<br>
&nbsp; return numberString<br>
end decimalToX</font></div>
<div><br></div>
<div><font face="Geneva" color="#000000">function xToDecimal
numberString, numberBase&nbsp; -- could benefit by using handles<br>
&nbsp; -- Returns the decimal equivalent of a numberString that uses
base numberBase notation<br>
&nbsp; put numberBase + 0 into myBase -- CompileIt types numberBase as
string...convert to integer<br>
&nbsp; if myBase &lt; 2 or myBase &gt; 35 then return &quot;Invalid
number base&quot;<br>
&nbsp; put 0 into theNumber<br>
&nbsp; put myBase + 55 into tooMany<br>
&nbsp; repeat with x =&nbsp; 1 to length(numberString)<br>
&nbsp;&nbsp;&nbsp; put myBase into theDigit<br>
&nbsp;&nbsp;&nbsp; get charToNum(char x of numberString)<br>
&nbsp;&nbsp;&nbsp; if it &gt; 47 and it &lt; 58 then put&nbsp; it - 48
into theDigit&nbsp;&nbsp; -- &quot;0&quot; to &quot;9&quot;<br>
&nbsp;&nbsp;&nbsp; else if it &gt; 64 and it &lt; tooMany then put it
- 55 into theDigit&nbsp; -- &quot;A&quot; to upper digit<br>
&nbsp;&nbsp;&nbsp; if theDigit &gt;= myBase then return &quot;Invalid
Digit&quot;<br>
&nbsp;&nbsp;&nbsp; put theNumber*myBase+theDigit into theNumber<br>
&nbsp; end repeat<br>
&nbsp; 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>
&quot;And I, which was two fooles, do so grow three;<br>
Who are a little wise, the best fooles bee.&quot;<br>
<br>
from &quot;The Triple Foole&quot; by John Donne (1572-1631)</div>
</body>
</html>