Convertir un nombre d'un base x en base 10

Contenu du snippet

Les valeurs possible pour la base vont de 2 a 37.

Source / Exemple :


'***********************************************************************************************************
' Name       : xBase2Dec
' Purpose    : Convert a number from a base x to a decimal number.
' Syntax     : xBase2Dec(Base, Number)
' Parameters : Base     : Base of the number to convert
'              Number   : Number to convert
' Return     : Decimal number
'***********************************************************************************************************
Public Function xBase2Dec(ByVal Base As Byte, ByVal Number As String) As String
    Dim bytPos    As Integer
    Dim bytLen    As Byte
    Dim bytValue  As Byte
    Dim strChar   As String * 1
    Dim lngResult As Long
    
    If Base > 1 And Base < 38 Then
        bytLen = Len(Number)
        For bytPos = bytLen To 1 Step -1
            strChar = Mid(Number, bytPos, 1)
            If IsNumeric(strChar) Then
                bytValue = strChar
            Else
                bytValue = Asc(strChar) - 55
            End If
            If bytValue > Base Then
                Err.Raise 6, , "The number to convert is bigger than the base."
            End If
            lngResult = lngResult + (bytValue * (Base ^ (bytLen - bytPos)))
        Next
        xBase2Dec = lngResult
    Else
        Err.Raise 6, , "Cannot convert from base " & Base
    End If
End Function

A voir également