This shows you the differences between two versions of the page.
|
developer:scriptsamples:uncommonconversions [2012/05/24] dale |
developer:scriptsamples:uncommonconversions [2012/05/24] (current) dale |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Uncommon Numeric Conversions ====== | ||
| + | > **Developer:** //[[developer:rhinoscript|RhinoScript]]// | ||
| + | > **Summary:** //Demonstrates some useful (or not so useful) numeric conversions in RhinoScript.// | ||
| + | =====Convert numbers to their hexadecimal representation===== | ||
| + | |||
| + | <code vb> | ||
| + | Function CHex(intNumber) | ||
| + | |||
| + | Dim strChars, intSign | ||
| + | |||
| + | strChars = "0123456789ABCDEF" | ||
| + | intSign = Sgn(intNumber) | ||
| + | |||
| + | intNumber = Fix(Abs(CDbl(intNumber))) | ||
| + | If (intNumber = 0) Then | ||
| + | CHex = "0" | ||
| + | Exit Function | ||
| + | End If | ||
| + | |||
| + | While (intNumber > 0) | ||
| + | CHex = Mid(strChars, 1 + (intNumber - 16 * Fix(intNumber / 16)), 1) & CHex | ||
| + | intNumber = Fix(intNumber / 16) | ||
| + | Wend | ||
| + | |||
| + | If (intSign = -1) Then CHex = "-" & CHex | ||
| + | |||
| + | End Function | ||
| + | |||
| + | </code> | ||
| + | |||
| + | > **Example:** //// | ||
| + | |||
| + | Rhino.Print CHex(2008) '7D8 | ||
| + | |||
| + | =====Convert numbers to their binary representation===== | ||
| + | |||
| + | <code vb> | ||
| + | Function CBinary(intNumber, intBits) | ||
| + | |||
| + | Dim strBinary, intMask, i | ||
| + | |||
| + | strBinary = | ||
| + | intMask = 1 | ||
| + | |||
| + | For i = 1 To intBits | ||
| + | If (intNumber And intMask) Then | ||
| + | strBinary = "1" & strBinary | ||
| + | Else | ||
| + | strBinary = "0" & strBinary | ||
| + | End If | ||
| + | intMask = intMask * 2 | ||
| + | Next | ||
| + | |||
| + | CBinary = strBinary | ||
| + | |||
| + | End Function | ||
| + | |||
| + | </code> | ||
| + | |||
| + | > **Example:** //// | ||
| + | |||
| + | Rhino.Print CBinary(2008, 16) '0000011111011000 | ||
| + | |||
| + | =====Convert numbers to their Roman numeral representation===== | ||
| + | |||
| + | <code vb> | ||
| + | Function CRoman(intNumber) | ||
| + | |||
| + | Dim v, w, x, y, arrOnes, arrTens, arrHund, arrThou | ||
| + | |||
| + | arrOnes = Array(,"I","II","III","IV","V","VI","VII","VIII","IX") | ||
| + | arrTens = Array(,"X","XX","XXX","XL","L","LX","LXX","LXXX","XC") | ||
| + | arrHund = Array(,"C","CC","CCC","CD","D","DC","DCC","DCCC","CM") | ||
| + | arrThou = Array(,"M","MM","MMM","MMMM","MMMMM") | ||
| + | |||
| + | v = ((intNumber - (intNumber Mod 1000)) / 1000) | ||
| + | intNumber = (intNumber Mod 1000) | ||
| + | |||
| + | w = ((intNumber - (intNumber Mod 100)) / 100) | ||
| + | intNumber = (intNumber Mod 100) | ||
| + | |||
| + | x = ((intNumber - (intNumber Mod 10)) / 10) | ||
| + | |||
| + | y = (intNumber Mod 10) | ||
| + | |||
| + | CRoman = arrThou(v) & arrHund(w) & arrTens(x) & arrOnes(y) | ||
| + | |||
| + | End Function | ||
| + | |||
| + | </code> | ||
| + | |||
| + | > **Example:** //// | ||
| + | |||
| + | Rhino.Print CRoman(2008) 'MMVIII | ||
| + | |||
| + | =====Convert Roman numeral representations to their base 10 representation===== | ||
| + | |||
| + | <code vb> | ||
| + | Function CUnRoman(strRoman) | ||
| + | |||
| + | Dim intvalue, strChar, i | ||
| + | intValue = 0 | ||
| + | |||
| + | If InStr(strRoman, "CM") Then | ||
| + | intValue = intValue + 900 | ||
| + | strRoman = Replace(strRoman, "CM", ) | ||
| + | End If | ||
| + | |||
| + | If InStr(strRoman, "CD") Then | ||
| + | intValue = intValue + 400 | ||
| + | strRoman = Replace(strRoman, "CD", ) | ||
| + | End If | ||
| + | |||
| + | If InStr(strRoman, "XC") Then | ||
| + | intValue = intValue + 90 | ||
| + | strRoman = Replace(strRoman, "XC", ) | ||
| + | End If | ||
| + | |||
| + | If InStr(strRoman, "XL") Then | ||
| + | intValue = intValue + 40 | ||
| + | strRoman = Replace(strRoman, "XL", ) | ||
| + | End If | ||
| + | |||
| + | If InStr(strRoman, "IX") Then | ||
| + | intValue = intValue + 9 | ||
| + | strRoman = Replace(strRoman, "IX", ) | ||
| + | End If | ||
| + | |||
| + | If InStr(strRoman, "IV") Then | ||
| + | intValue = intValue + 4 | ||
| + | strRoman = Replace(strRoman, "IV", ) | ||
| + | End If | ||
| + | |||
| + | For i = 1 To Len(strRoman) | ||
| + | strChar = Mid(strRoman, i, 1) | ||
| + | Select Case strChar | ||
| + | Case "I" intValue = intValue + 1 | ||
| + | Case "V" intValue = intValue + 5 | ||
| + | Case "X" intValue = intValue + 10 | ||
| + | Case "L" intValue = intValue + 50 | ||
| + | Case "C" intValue = intValue + 100 | ||
| + | Case "D" intValue = intValue + 500 | ||
| + | Case "M" intValue = intValue + 1000 | ||
| + | End Select | ||
| + | Next | ||
| + | |||
| + | CUnRoman = intValue | ||
| + | |||
| + | End Function | ||
| + | |||
| + | </code> | ||
| + | |||
| + | > **Example:** //// | ||
| + | |||
| + | Rhino.Print CUnRoman(MMVIII) '2008 | ||
| + | |||
| + | \\ | ||
| + | |||
| + | {{tag>Developer RhinoScript}} | ||