Public Function DecimalToAny(ByVal Number As Long, ByVal Base As Short)_
As String
Dim strNumberOutput As String
'These bounds are in place because they will hold the Long's
'largest possible value.
'An Int16 and String array are both used for readability.
'You only need to use a string array
Dim intRemander(36) As Int16
Dim strRemander(36) As String
Dim IsNegative As Boolean
Dim i As Int16
'Roman Numerals
If Base = 0 Then
Return ToRomanNumeral(Number)
End If
'Supported Bases (1-9 A-Z)
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)")
'Return 0 unless Unary (there is no number 0 in unary)
If Number = 0 And Base <> 1 Then
Return "0"
ElseIf Number = 0 And Base = 1 Then
Return ""
End If
'If needed, Adds the Negative sign after computation
If Number < 0 Then
IsNegative = True
Number *= -1
End If
'Special circumstances for Unary
If Base = 1 Then
Dim strUnary As String = ""
If IsNegative = True Then
strUnary = "-"
End If
'Think tick marks
For i = 0 To Number - 1
strUnary &= "1"
Next
Return strUnary
Exit Function
End If
'Count the remainders
Do Until Number = 0
intRemander(i) = Number Mod Base
strRemander(i) = intRemander(i).ToString
Number -= Number Mod Base
Number = (Number / Base)
i += 1
Loop
'Take off unused elements
ReDim Preserve intRemander(i - 1)
ReDim Preserve strRemander(i - 1)
'Converts > 9 to Text (10 = A)
For i = 0 To intRemander.GetUpperBound(0)
If Int(strRemander(i)) > 9 Then
strRemander(i) = Chr(strRemander(i) + 55)
End If
Next
'Concatinate the Remanders backwards
For i = intRemander.GetUpperBound(0) To 0 Step -1
strNumberOutput &= strRemander(i)
Next
'If needed, add that negative sign
If IsNegative = True Then strNumberOutput = "-" & strNumberOutput
Return strNumberOutput
End Function
Number Systems
- The Different Systems
- Any To Decimal
- Decimal To Any
- Any To Any
- Roman Numerals
Decimal To Any
You might also like...
VB.NET books
-
Programming with Microsoft Visual Basic 2008
Programming with Microsoft Visual Basic 2008, Fourth Edition by the best-selling author, Diane Zak, is designed for a first course in programming. Using the most recent version of the software, Visual Basic 2008, this book teaches individuals how to ...
VB.NET forum discussion
-
CorelDRAW VBA: cdrTraceLineDrawing FAILS, producing single linear path instead of Centerline trace?
by dancemanj (0 replies)
-
client/server application using activex
by beautifulheart (0 replies)
-
System Error &H8007007E. The specifed module could not be found.
by swiftsafe (5 replies)
-
Invitation to take part in an academic research study
by researchlab (0 replies)
-
Send SMS with SMPP
by mmahmoud (0 replies)
VB.NET podcasts
-
Stack Overflow Podcast: Podcast #45 – Keeping it Sharp
Published 7 years ago, running time 0h54m
Our guest this week is Eric Lippert – language architect extraordinaire and famous for all his work at Microsoft in developing their languages Eric joined Microsoft right out of college and was originally working on VB It’s time for everyone’s favorite game: Name the Worst Feature of that Microso.
Comments