Conversion Décimale/binaire

Ces deux fonctions permettent de transformer un nombre décimal en chaîne de caractère binaire, et l'autre permet de transformer une chaîne de caractère binaire en nombre décimal

Cette procédure Test sert simplement à montrer l'utiolisation des deux fonctions

Sub Test()
  MsgBox BinaireVersDecimal("00100011")
  MsgBox DecimalVersBinaire(224)
End Sub

Fonction permettant de convertir une chaîne texte binaire vers un nombre décimal :

Function BinaireVersDecimal(ChaineBinaire As String) As Long
  Dim LongueurChaine As Long, Parcourir As Long
  Dim Resultat As Double, LongueurChaine2 As Double
  Dim ChaineTemporaire As String
  
  LongueurChaine = Len(ChaineBinaire)
  ChaineTemporaire = StrReverse(ChaineBinaire)
  LongueurChaine2 = 1
  
  For Parcourir = 1 To LongueurChaine
    Resultat = Resultat + CLng(Mid$(ChaineTemporaire, Parcourir, 1)) * LongueurChaine2
    LongueurChaine2 = LongueurChaine2 * 2
  Next Parcourir
  
  If Resultat > 2147483647 Then ' Négatifs
    BinaireVersDecimal = Resultat - 4294967295#
  Else
    BinaireVersDecimal = Resultat
  End If
End Function

Fonction permettant de convertir un nombre décimal binaire vers une chaîne texte:

Function DecimalVersBinaire(NombreDecimal As Long) As String
  Dim Resultat As String
  Dim LeDecimal As Long, TraiteNegatif As Long
  
  If NombreDecimal < 0 Then TraiteNegatif = 1 Else TraiteNegatif = 0
  
  LeDecimal = Abs(NombreDecimal)
  Resultat = ""
  
  Do While LeDecimal <> 0
    Resultat = (LeDecimal + TraiteNegatif) Mod 2 & Resultat
    LeDecimal = LeDecimal \ 2
  Loop
  
   DecimalVersBinaire = Right$(String$(10, CStr(TraiteNegatif)) & Resultat, 10)
End Function

Les chiffres 10 inddiqués en rouge représentent la liongueur de la chaîne binaire renvoyée. Si on voulait un retour sur 15 bits, il faudrait remplacer 10 par 15