Convert to Decimal
Submitted By:
Nad__Af
Rating:
Not rated (
Rate It)
Visual Basic Todecimal function
The following function converts a number written in radix xRad to its equivalent decimal representation. You can even convert large numbers such as "1000000100111111001110010111100011111000100101000000100110000100001111111111111111111111111111" (94-digit in binary) to "9999999999999999999999999999" (28-digit in decimal).
However, if you want to convert numbers from radix over 10, you have to assign the symbols manually:
Case Is = "E"
dVal = 14
Case Is = "F"
dVal = 15
//your code goes here.
You continue assigning normally using the "Select Case" statement.
See also ToRadix and ToRadix2 function.
By: Nadeem Afanah
E-mail: [[Email Removed]]
'==============================================================
'Visual Basic version
'Created by Nadeem Afanah
'e-mail: [[Email Removed]]
'The following function converts any number written in radix xRad to its equivalent
'decimal representation.
Function ToDecimal(sNum As String, xRad As Long) As Variant
Dim I As Long
Dim dRes As Variant
Dim sVal As String
Dim dVal As Variant
Dim lLength As Long
dRes = CDec(dRes)
dVal = CDec(dVal)
lLength = Len(sNum)
For I = 1 To lLength
sVal = Mid$(sNum, lLength - I + 1, 1)
Select Case sVal
Case Is = "A"
dVal = 10
Case Is = "B"
dVal = 11
Case Is = "C"
dVal = 12
Case Is = "D"
dVal = 13
Case Is = "E"
dVal = 14
Case Is = "F"
dVal = 15
'TODO: add your symbols here.
Case Else
dVal = Val(sVal)
End Select
dRes = dRes + pow(xRad, (I - 1)) * dVal
Next I
ToDecimal = dRes
End Function
'The following function is needed to calculate powers of numbers.
Function pow(ByVal Base As Long, ByVal Num As Long) As Variant
Dim I As Long
pow = CDec(Base)
If Num = 0 Then
pow = 1
Exit Function
End If
For I = 1 To Num - 1
pow = pow * Base
Next
End Function
'==============================================================================
'______________________________________________________________________________
'Visual Basic .NET version
'Created by Nadeem Afanah
'E-mail: [[Email Removed]]
'The following function converts any number written in radix xRad to its equivalent
'decimal representation.
Function ToDecimal(ByVal sNum As String, ByVal xRad As Long) As Decimal
Dim I As Long
Dim dRes As Decimal
Dim sVal As String
Dim dVal As Decimal
Dim lLength As Long
lLength = Len(sNum)
For I = 1 To lLength
sVal = Mid$(sNum, lLength - I + 1, 1)
Select Case sVal
Case Is = "A"
dVal = 10
Case Is = "B"
dVal = 11
Case Is = "C"
dVal = 12
Case Is = "D"
dVal = 13
Case Is = "E"
dVal = 14
Case Is = "F"
dVal = 15
'TODO: add your symbols here.
Case Else
dVal = Val(sVal)
End Select
dRes = dRes + pow(xRad, (I - 1)) * dVal
Next I
ToDecimal = dRes
End Function
'The following function is needed to calculate powers of numbers.
Function pow(ByVal Base As Long, ByVal Num As Long) As Decimal
Dim I As Long
pow = CDec(Base)
If Num = 0 Then
pow = 1
Exit Function
End If
For I = 1 To Num - 1
pow = pow * Base
Next
End Function
'==============================================================================