Convertir un nombre decimal (base 10) en base x

Contenu du snippet

Les valeurs possible pour la base vers laquelle se fait la conversion vont de 2 a 37.

Source / Exemple :


'***********************************************************************************************************
' Name       : xDec2Base
' Purpose    : Convert a decimal number to a number in base x.
' Syntax     : xDec2Base(Base, Number)
' Parameters : Base     : Base to convert to
'              Number   : Number to convert
' Return     : Number in base 'Base'
'***********************************************************************************************************
Public Function xDec2Base(ByVal Base As Byte, ByVal Number As Long) As String
    Dim strResult As String
    Dim strR      As String
    
    If Base > 1 And Base < 38 Then
        Do
            strR = Number Mod Base
            If strR > 9 Then
                strR = Chr(55 + strR)
            End If
            strResult = strR & strResult
            Number = Number  Base
        Loop While Not Number = 0
        xDec2Base = strResult
    Else
        Err.Raise 6, , "Cannot convert to base " & Base
    End If
End Function

A voir également