|
-
Mar 21st, 2012, 04:00 AM
#9
Thread Starter
Addicted Member
Decimal to any radix and back again
Next is how to write functions to transform decimal numbers to any radix (base) 2-62 and then back again.
It is now relatively easy to extend all of the previously explicated functions to allow transformation to and from any base 2-62 (where base 62 [0-9][A-Z][a-z]) which can also handle large numbers via the decimal datatype. The next function transforms a decimal number into any radix 2-62:
vb Code:
Function DecTo(ByVal x, ByVal Radix&) As String
' Witis transforms up to 27 digit positive integer from decimal to any base 2-62
If (Radix < 2) Or (Radix > 62) Then Exit Function
Dim y&: x = CDec(x)
Do
y = (x / Radix - Fix(x / Radix)) * Radix ' get dec value of the rightmost column given the radix
If y < 10 Then
DecTo = y & DecTo ' add a numerical digit
ElseIf y < 36 Then DecTo = Chr$(y + 55) & DecTo ' non num digit [A-Z]
ElseIf y < 62 Then DecTo = Chr$(y + 61) & DecTo ' non num digit [a-z]
End If
x = Fix(x / Radix) ' move the point to the left one place and discard the remainder
Loop While x
End Function
This function determines the decimal value of the rightmost column given the radix and then converts it into the alpha numeric code [0-9][A-Z][a-z], then moves the point one place to the left until the entire number is transformed from decimal to the chosen radix.
For example 55 decimal to Hex which is (3A):
The first iteration works out that the rightmost column contains 0.625 of the radix or 10 which is represented by the letter A in hex.
The next iteration determines that the rightmost column contains 0.1875 of the radix or 3 which is also represented by 3 in hex.
The next function allows numbers from other radixes 2-62 to be transformed back into decimal:
Code:
Function ToDec(ByVal x, ByVal Radix&) As Variant
' Witis transforms any postive whole number from any base 2-62 to a decimal value
If (Radix < 2) Or (Radix > 62) Then Exit Function
Dim ba() As Byte, i&, bitval As Variant, b As Byte
bitval = 1
ba = StrConv(x, vbFromUnicode)
For i = UBound(ba) To 0 Step -1
b = ba(i)
If b < 58 Then
ToDec = CDec(ToDec + (b - 48) * bitval)
ElseIf b < 91 Then ToDec = CDec(ToDec + (b - 55) * bitval)
ElseIf b < 123 Then ToDec = CDec(ToDec + (b - 61) * bitval)
End If
bitval = CDec(bitval * Radix)
Next i
End Function
This function checks each bit from right to left and works out it's numerical value in decimal, for example A in hex is 10 decimal. Then it simply multiplies this number by the bit value.
For example:
2A in hex
A = 10 decimal * bitvalue = 10 * 16^0 = 10
2 = 2 decimal * bitvalue = 2 * 16^1 = 32
so the answer 2A hex = 42 decimal
What about Octet?
67 Octet
7 = 7 * 8^0 = 7
6 = 6 * 8^1 = 48
so the answer is 55 decimal
Example usage to convert a 27 digit integer to every base 2-62 and back again; output is to the debug window (ctrl+g):
Code:
Private Sub Command1_Click()
Dim res, lbase&, i&
For i = 2 To 62 Step 1
lbase = i
res = DecTo("888888888888888888888888888", lbase)
Debug.Print res
res = ToDec(res, lbase)
Debug.Print res
Next i
End Sub
If you can understand these functions, good job, you can now transform positive integers from decimal to any radix and back again including binary, hex and octal.
All men have an inherent right to life, the right to self determination including freedom from forced or compulsory labour, a right to hold opinions and the freedom of expression, and the right to a fair trial and freedom from torture. Be aware that these rights are universal and inalienable (cannot be given, taken or otherwise transferred or removed) although you do risk losing the aforementioned rights should you fail to uphold them e.g Charles Taylor; United Nations sources: http://www.un.org/en/documents/udhr/, http://www.ohchr.org/EN/Professional...ages/CCPR.aspx. Also Charles I was beheaded on the 30th of January of 1649 for trying to replace parliamentary democracy with an absolute monarchy, the same should happen to Dr Phil and Stephen Fry; source: http://www.vbforums.com/showthread.p...ute-Monarchism.
The plural of sun is stars you Catholic turkeys.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|