Decimal To Any
Converting from the Decimal system to others invloves some slightly
complex math. To fully understand this code you will need to
understand how
Mod
functions.
Public Function DecimalToAny(ByVal Number As Long, ByVal Base As Short)_
As String
Dim strNumberOutput As String
Dim intRemander(36) As Int16
Dim strRemander(36) As String
Dim IsNegative As Boolean
Dim i As Int16
If Base = 0 Then
Return ToRomanNumeral(Number)
End If
If Base > 36 Or Base < 0 Then Throw New_
Exception("Start Base can only be 0 through 36_
(0-9, A-Z, or Roman Numerals)")
If Number = 0 And Base <> 1 Then
Return "0"
ElseIf Number = 0 And Base = 1 Then
Return ""
End If
If Number < 0 Then
IsNegative = True
Number *= -1
End If
If Base = 1 Then
Dim strUnary As String = ""
If IsNegative = True Then
strUnary = "-"
End If
For i = 0 To Number - 1
strUnary &= "1"
Next
Return strUnary
Exit Function
End If
Do Until Number = 0
intRemander(i) = Number Mod Base
strRemander(i) = intRemander(i).ToString
Number -= Number Mod Base
Number = (Number / Base)
i += 1
Loop
ReDim Preserve intRemander(i - 1)
ReDim Preserve strRemander(i - 1)
For i = 0 To intRemander.GetUpperBound(0)
If Int(strRemander(i)) > 9 Then
strRemander(i) = Chr(strRemander(i) + 55)
End If
Next
For i = intRemander.GetUpperBound(0) To 0 Step -1
strNumberOutput &= strRemander(i)
Next
If IsNegative = True Then strNumberOutput = "-" & strNumberOutput
Return strNumberOutput
End Function