Convertir hexadécimal en binaire

Résolu
julien -  
 julien -
Bonjour,

Je suis en pleine réflexion sur un code pour transposer un code hexadécimale en binaire ,
j'ai fait un code qui fonctionne mais qui est un peu " Barbare " avez vous une solution de simplification pour qu'il soit moins lourd ?

Lien : http://www.cjoint.com/c/FHjkLt5olxp

Merci
A voir également:

4 réponses

pijaku Messages postés 12263 Date d'inscription   Statut Modérateur Dernière intervention   2 761
 
Bonjour,

Tout d'abord, pour que tout le monde puisse suivre, je place ici ton code :
Sub test()

               
               a = Mid(Cells(1, 3).Value, 1, 1)  ' la variable a prend la valeur du premier caractère
               b = Mid(Cells(1, 3).Value, 2, 1)  ' la variable b prend la valeur du deuxieme caractère
               c = Mid(Cells(1, 3).Value, 3, 1)  ' la variable c prend la valeur du troisieme caractère
               D = Mid(Cells(1, 3).Value, 4, 1)  ' la variable d prend la valeur du quatrieme caractère
    
' le premier caractère

If a = 0 Then
resultat = resultat & "0000"
Else
If a = 1 Then
resultat = resultat & "0001"
Else
If a = 2 Then
resultat = resultat & "0010"
Else
If a = 3 Then
resultat = resultat & "0011"
Else
If a = 4 Then
resultat = resultat & "0100"
Else
If a = 5 Then
resultat = resultat & "0101"
Else
If a = 6 Then
resultat = resultat & "0110"
Else
If a = 7 Then
resultat = resultat & "0111"
Else
If a = 8 Then
resultat = resultat & "1000"
Else
If a = 9 Then
resultat = resultat & "1001"
Else
If a = "A" Then
resultat = resultat & "1010"
Else
If a = "B" Then
resultat = resultat & "1011"
Else
If a = "C" Then
resultat = resultat & "1100"
Else
If a = "D" Then
resultat = resultat & "1101"
Else
If a = "E" Then
resultat = resultat & "1110"
Else
If a = "F" Then
resultat = resultat & "1111"

End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

' le second caractère

If b = 0 Then
resultat = resultat & "0000"
Else
If b = 1 Then
resultat = resultat & "0001"
Else
If b = 2 Then
resultat = resultat & "0010"
Else
If b = 3 Then
resultat = resultat & "0011"
Else
If b = 4 Then
resultat = resultat & "0100"
Else
If b = 5 Then
resultat = resultat & "0101"
Else
If b = 6 Then
resultat = resultat & "0110"
Else
If b = 7 Then
resultat = resultat & "0111"
Else
If b = 8 Then
resultat = resultat & "1000"
Else
If b = 9 Then
resultat = resultat & "1001"
Else
If b = "A" Then
resultat = resultat & "1010"
Else
If b = "B" Then
resultat = resultat & "1011"
Else
If b = "C" Then
resultat = resultat & "1100"
Else
If b = "D" Then
resultat = resultat & "1101"
Else
If b = "E" Then
resultat = resultat & "1110"
Else
If b = "F" Then
resultat = resultat & "1111"

End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

' le troisième caractère

If c = 0 Then
resultat = resultat & "0000"
Else
If c = 1 Then
resultat = resultat & "0001"
Else
If c = 2 Then
resultat = resultat & "0010"
Else
If c = 3 Then
resultat = resultat & "0011"
Else
If c = 4 Then
resultat = resultat & "0100"
Else
If c = 5 Then
resultat = resultat & "0101"
Else
If c = 6 Then
resultat = resultat & "0110"
Else
If c = 7 Then
resultat = resultat & "0111"
Else
If c = 8 Then
resultat = resultat & "1000"
Else
If c = 9 Then
resultat = resultat & "1001"
Else
If c = "A" Then
resultat = resultat & "1010"
Else
If c = "B" Then
resultat = resultat & "1011"
Else
If c = "C" Then
resultat = resultat & "1100"
Else
If c = "D" Then
resultat = resultat & "1101"
Else
If c = "E" Then
resultat = resultat & "1110"
Else
If c = "F" Then
resultat = resultat & "1111"

End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

' le quatrieme caractère

If D = 0 Then
resultat = resultat & "0000"
Else
If D = 1 Then
resultat = resultat & "0001"
Else
If D = 2 Then
resultat = resultat & "0010"
Else
If D = 3 Then
resultat = resultat & "0011"
Else
If D = 4 Then
resultat = resultat & "0100"
Else
If D = 5 Then
resultat = resultat & "0101"
Else
If D = 6 Then
resultat = resultat & "0110"
Else
If D = 7 Then
resultat = resultat & "0111"
Else
If D = 8 Then
resultat = resultat & "1000"
Else
If D = 9 Then
resultat = resultat & "1001"
Else
If D = "A" Then
resultat = resultat & "1010"
Else
If D = "B" Then
resultat = resultat & "1011"
Else
If D = "C" Then
resultat = resultat & "1100"
Else
If D = "D" Then
resultat = resultat & "1101"
Else
If D = "E" Then
resultat = resultat & "1110"
Else
If D = "F" Then
resultat = resultat & "1111"

End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

MsgBox (resultat)

    
End Sub

1
pijaku Messages postés 12263 Date d'inscription   Statut Modérateur Dernière intervention   2 761
 
Ensuite, ma réponse :
1- pourquoi 4 caractères? 15AACF7 n'est-il pas un Hexadécimal selon toi?
Pour pallier à cela, il convient de boucler sur chacun des caractères de ton Hexa, comme ceci :
For i = 1 To Len(TonHexa) 
    Debug.Print Mid(TonHexa, i, 1)
Next i

2- le reste étant identique quelque soit le caractère (If caractere = 0 Then ...), tu peux en faire une Fonction :

    Private Function Car_Hex_En_Bin(Carac As String) As String
        Select Case Carac 
            Case "0": Car_Hex_En_Bin = "0000" 
            Case "1": Car_Hex_En_Bin = "0001" 
            Case "2": Car_Hex_En_Bin = "0010" 
            Case "3": Car_Hex_En_Bin = "0011" 
            Case "4": Car_Hex_En_Bin = "0100" 
            Case "5": Car_Hex_En_Bin = "0101" 
            Case "6": Car_Hex_En_Bin = "0110" 
            Case "7": Car_Hex_En_Bin = "0111" 
            Case "8": Car_Hex_En_Bin = "1000" 
            Case "9": Car_Hex_En_Bin = "1001" 
            Case "A": Car_Hex_En_Bin = "1010" 
            Case "B": Car_Hex_En_Bin = "1011" 
            Case "C": Car_Hex_En_Bin = "1100" 
            Case "D": Car_Hex_En_Bin = "1101" 
            Case "E": Car_Hex_En_Bin = "1110" 
            Case "F": Car_Hex_En_Bin = "1111" 
        End Select
    End Function


Ne te reste plus qu'à intégrer cette fonction à ta boucle :

Sub Test()
Dim TonHexa As String, Resultat As String, i As Integer

    TonHexa = Cells(1, 3).Value
    For i = 1 To Len(TonHexa) 
        Resultat = Resultat &  Car_Hex_En_Bin(Mid(TonHexa, i, 1))
    Next i
    MsgBox Resultat
End Sub

0
blux Messages postés 27104 Date d'inscription   Statut Modérateur Dernière intervention   3 359
 
Salut,

le fonction HEXBIN() ne fonctionne pas chez toi ?
0
julien
 
Merci pijaku , comme toujours tu es irréprochable ;)

Merci beaucoup et à bientôt !
0