Convert numerals
Luciano wrote:
Does somebody have any idea about how to develop some VBA function or
macro aiming conversion of several numerals types and that could be used
for small and large numbers(e.g. 100000000000 in decimal)? The major
numerals base for which I need this conversions a
Binary (Base 2)
Ternary (Base 3)
Quaternary (Base 4)
Quinary (Baase 5)
Senary (Base 6)
Septenary (Base 7)
Octal (Base 8)
Nonary (Base 9)
Decimal (Base 10)
Undecimal (Base 11)
Duodecimal (Base 12)
Base 13
Hexadecimal (Base 16)
Vigesimal (Base 20)
and others...
Homework, anyone?
Function toBase(ByVal what As Variant, base As Long) As String
'Should be able to handle any decimal number to the limits of a variant,
'and bases from 2 to 36.
If (base < 2) Or (base 36) Or (Int(what) < what) Then Exit Function
Dim tmp As String
Static digits As Variant
If IsEmpty(digits) Then digits = Array("0", "1", "2", "3", "4", "5", _
"6", "7", "8", "9", "A", "B", _
"C", "D", "E", "F", "G", "H", _
"I", "J", "K", "L", "M", "N", _
"O", "P", "Q", "R", "S", "T", _
"U", "V", "W", "X", "Y", "Z")
While (what < 0)
tmp = digits(what Mod base) & tmp
what = what \ base
Wend
toBase = tmp
End Function
For base 37+ add your extra digits to the end of the array and change 36 to
your new max.
--
They'd just stopped trusting anything they couldn't see.
|