Any2Dec

Converts a number of any base to decimal.
Hexa to decimal, octa to decimal, binary, etc.

CodeFunctionName
What is this?

Public

Tested

Imported
Function Any2Dec(ByVal ConvertNumber As String, Optional ByVal base As Integer = 16) As Long
    Dim index As Long
    Dim digits As String
    Dim digitValue As Long
    Rett = ConvertNumber
    If base < 2 Or base > 36 Then Goto ByeBye ' Err.Raise 5 ' check base
    digits = Left("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", base) ' get the list of valid digits
    For index = 1 To Len(ConvertNumber)    ' convert to decimal
        digitValue = InStr(1, digits, Mid$(ConvertNumber, index, 1), vbTextCompare) - 1 ' get the digit's value
        If digitValue < 0 Then Goto ByeBye ' Err.Raise 5 ' error if invalid digit
        Rett = Rett * base + digitValue ' add to running result
    Next
    ByeBye:
    Any2Dec = Rett
End Function

ByVal ConvertNumber As String, Optional ByVal base As Integer = 16

Example    Any2Dec("4F320D") =
Any2Dec("010001", 2) =
Any2Dec("012401", 5) =
Any2Dec("20180828", 10) = 20180828

Views 105

Downloads 55

CodeID
DB ID

ANmarAmdeen
604
Attachments
Revisions

v2.0

Wednesday
October
11
2023