Library tutorials & articles
Number Systems
By Mitch Dusina, published on 16 Dec 2005
Page 2 of 5
- The Different Systems
- Any To Decimal
- Decimal To Any
- Any To Any
- Roman Numerals
Any To Decimal
Since there is no real way to express
G1 Septendecimal in Visual
Studio, we use the String variable type to hold a textual
representation of it. Regardless of the base, there is a general
equation for converting it to decimal by calculating each character's
base weight and then adding the individual values together.
Public Function AnyToDecimal(ByVal Number As String, ByVal Base As_
Short) As Long
Dim lDecimalOutput As Long
Dim IsNegative As Boolean
Dim i As Short
'Lower case a-z may be used to represent something different
'then their upper case counterparts if desired<
Number = Number.ToUpper()
'Roman Numerals (Roman Numerals have no Base technically
'and are really just an addition problem which needs to be
'dealt with in a different way)
If Base = 0 Then
Return FromRomanNumeral(Number)
End If
'Determine if the string is negative
If Number.StartsWith("-") = True Then
IsNegative = True
Number = Number.Replace("-", "")
End If
'Remove Invalid Characters
Dim chrTemp() As Char = Number.ToCharArray
Number = ""
For i = 0 To chrTemp.Length - 1
'Only accept numbers and characters
If (IsNumber(chrTemp(i).ToString) = True) Or_
(IsLetter(chrTemp(i).ToString) = True) Then
Number &= chrTemp(i).ToString
End If
Next
Dim chrNumberSplit() As Char = Number.ToCharArray
Dim lNumberSplit(chrNumberSplit.GetUpperBound(0)) As Long
'Supported Bases (1-9 A-Z currently. This behavior can be_
'modified to allow "AA" to come after "Z", etc.)
If Base < 0 Then Throw New Exception("Starting Base must be_
Greater Than or Equal to 0.")
'Add the value of each symbol to an array (9 = 9, A = 10,_
'B = 11, etc.)
For i = 0 To chrNumberSplit.GetUpperBound(0)
If Char.IsNumber(chrNumberSplit(i)) Then
lNumberSplit(i) = Int(chrNumberSplit(i).ToString)
Else
'Convert letters to numbers (A = 10, etc.)
lNumberSplit(i) = Asc(chrNumberSplit(i)) - 55
End If
'Checks for "overflow" chacacters (Base 1 is different_
'because the '1' chacter is used instead of the 0)
If lNumberSplit(i) > Base - 1 AndAlso Base <> 1 Then
Throw New Exception(chrNumberSplit(i) & " is not a valid_
character in a " & GetBaseName(Base) & " number system.")
ElseIf Base = 1 AndAlso lNumberSplit(i) <> 1 Then
Throw New Exception(chrNumberSplit(i) & " is not a valid_
character in a " & GetBaseName(Base) & " number system.")
End If
Next
Try
'Calculate the Base weight of each array item
For i = 0 To chrNumberSplit.GetUpperBound(0)
lNumberSplit(i) = lNumberSplit(i) * Base ^_
(chrNumberSplit.GetUpperBound(0) - i)
Next
'Add the weighted values together to produce a Decimal_
'representation
For i = 0 To chrNumberSplit.GetUpperBound(0)
lDecimalOutput += lNumberSplit(i)
Next
'If the original number was negative, reflect it
If IsNegative = True Then
lDecimalOutput *= -1
End If
Catch
Throw New Exception("[" & Number & "] " & GetBaseName(Base)_
& " requires more than 64-bits in Decimal form to represent_
(Overflow).")
End Try
Return lDecimalOutput
End Function
The preceding code requires these self explainitry Functions:
Public Function IsNumber(ByVal TestString As String) As Boolean
Dim chrString() As Char = TestString.ToCharArray
Dim i0, i9 As Short
Dim i As Integer
i0 = Asc("0")
i9 = Asc("9")
For i = 0 To TestString.Length - 1
If (Asc(chrString(i)) < i0 Or Asc(chrString(i)) > i9) Then
Return False
Exit Function
End If
Next
Return True
End Function
Public Function IsLetter(ByVal TestString As String) As Boolean
Dim chrString() As Char = TestString.ToCharArray
Dim iLowerA, iLowerZ, iUpperA, iUpperZ As Short
Dim i As Integer
iLowerA = Asc("a")
iLowerZ = Asc("z")
iUpperA = Asc("A")
iUpperZ = Asc("Z")
For i = 0 To TestString.Length - 1
If (Asc(chrString(i)) >= iLowerA And Asc(chrString(i)) <= iLowerZ)_
Or (Asc(chrString(i)) >= iUpperA And Asc(chrString(i)) <= iUpperZ) Then
Else
Return False
Exit Function
End If
Next
Return True
End Function
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
It looks good, the only thing I can spot is the line where you catch the Exception. Not finding an extended numeral is expected (and common at that), so an Exception shouldn't be used to control the flow of code.
Although I can't think of an easy way around it since they removed Enum.TryParse (2.0 and later I think).
private int ParseRomanNumeral(string numeral)
{
numeral = numeral.ToUpper();
char[] characters = numeral.ToCharArray();
int[] parseBuf = new int[numeral.Length];
int result = 0;
bool bSkip = false;
for (int i = numeral.Length - 1; i >= 0; i--)
{
if (bSkip)
{
bSkip = false; // Skip this digit because it is part of a 2-digit numeral
continue;
}
parseBuf[i] = (int)Enum.Parse(typeof(RomanNumerals), characters[i].ToString());
if (parseBuf[i] % 5 == 0 && i > 0)
{
string extended = characters[i-1].ToString() + characters[i].ToString();
try
{
parseBuf[i] = (int)Enum.Parse(typeof(RomanNumerals), extended);
bSkip = true;
}
catch(ArgumentException)
{
// Ignore this exception. It just means we didn't find an extended numeral
}
}
}
for (int i = 0; i < numeral.Length; i++)
{
result += parseBuf[i];
}
return result;
}
private string ConvertToRomanNumeral(int num)
{
string sResult = "";
sResult = Enum.GetName(typeof(RomanNumerals), num);
if (sResult == null)
{
string[] names = Enum.GetNames(typeof(RomanNumerals));
int[] vals = (int[])Enum.GetValues(typeof(RomanNumerals));
for (int i = names.Length - 1; i > 0; i--) // Don't process the 0th element, since it is 0 and results in bad math
{
int temp = num / vals[i];
num -= temp * vals[i];
for (int j = 0; j < temp; j++)
{
sResult += names[i];
}
}
}
return sResult;
}
This thread is for discussions of Number Systems.