- ' CONVERSION D'UNE CHAINE BINAIRE EN VALEUR DECIMALE
- '
- ' Binary : du type "100111001" (longeur max 32 caracteres)
- ' NbBits : nombre de bits a considérer
- ' Signed : la chaine binaire désigne une valeur décimale signée ou non
- Public Function BinaryToDecimal(Binary As String, NbBits As Byte, Signed As Boolean) As Long
- Dim i As Byte
- BinaryToDecimal = 0
- If NbBits <= 1 Or NbBits > 32 Then Exit Function
- If Len(Binary) > NbBits Or Len(Binary) > 32 Then Exit Function
- Binary = Right("00000000000000000000000000000000" & Binary, NbBits)
- If Signed = False Then
- For i = 0 To Len(Binary) - 1
- BinaryToDecimal = BinaryToDecimal + Val(Mid(Binary, Len(Binary) - i, 1)) * 2 ^ i
- Next i
- Else
- For i = 0 To Len(Binary) - 2
- BinaryToDecimal = BinaryToDecimal + Val(Mid(Binary, Len(Binary) - i, 1)) * 2 ^ i
- Next i
- If Left(Binary, 1) = "1" Then
- BinaryToDecimal = -((2 ^ (NbBits - 1)) - BinaryToDecimal)
- End If
- End If
- End Function
-
- ' CONVERSION D'UNE VALEUR DECIMALE EN CHAINE BINAIRE
- '
- ' Decimal : valeur décimale à convertir
- ' NbBits : nombre de bits a considérer
- ' Completed : la chaine binaire est complétée de 0 si necéssaire
- Public Function DecimalToBinary(Decimale As Long, NbBits As Byte, Optional Completed As Boolean) As String
- Dim L, B As String
- DecimalToBinary = ""
- If Decimale > (2 ^ NbBits) - 1 Then Exit Function
- If Decimale < -(2 ^ (NbBits - 1)) - 1 Then Exit Function
- If NbBits > 32 Then Exit Function
-
- If Decimale < 0 Then
- Decimale = 2 ^ NbBits + Decimale
- End If
-
- If Decimale = 0 Or Decimale = 1 Then L = "" Else L = 1
- Do
- If Decimale Mod 2 = 0 Then B = "0" Else B = "1"
- DecimalToBinary = DecimalToBinary & B
- Decimale = Decimale \ 2
- Loop Until Decimale <= 1
- DecimalToBinary = StrReverse(DecimalToBinary & L)
- If Completed = True Then
- DecimalToBinary = Right("00000000000000000000000000000000" & DecimalToBinary, NbBits)
- End If
- End Function
' CONVERSION D'UNE CHAINE BINAIRE EN VALEUR DECIMALE
'
' Binary : du type "100111001" (longeur max 32 caracteres)
' NbBits : nombre de bits a considérer
' Signed : la chaine binaire désigne une valeur décimale signée ou non
Public Function BinaryToDecimal(Binary As String, NbBits As Byte, Signed As Boolean) As Long
Dim i As Byte
BinaryToDecimal = 0
If NbBits <= 1 Or NbBits > 32 Then Exit Function
If Len(Binary) > NbBits Or Len(Binary) > 32 Then Exit Function
Binary = Right("00000000000000000000000000000000" & Binary, NbBits)
If Signed = False Then
For i = 0 To Len(Binary) - 1
BinaryToDecimal = BinaryToDecimal + Val(Mid(Binary, Len(Binary) - i, 1)) * 2 ^ i
Next i
Else
For i = 0 To Len(Binary) - 2
BinaryToDecimal = BinaryToDecimal + Val(Mid(Binary, Len(Binary) - i, 1)) * 2 ^ i
Next i
If Left(Binary, 1) = "1" Then
BinaryToDecimal = -((2 ^ (NbBits - 1)) - BinaryToDecimal)
End If
End If
End Function
' CONVERSION D'UNE VALEUR DECIMALE EN CHAINE BINAIRE
'
' Decimal : valeur décimale à convertir
' NbBits : nombre de bits a considérer
' Completed : la chaine binaire est complétée de 0 si necéssaire
Public Function DecimalToBinary(Decimale As Long, NbBits As Byte, Optional Completed As Boolean) As String
Dim L, B As String
DecimalToBinary = ""
If Decimale > (2 ^ NbBits) - 1 Then Exit Function
If Decimale < -(2 ^ (NbBits - 1)) - 1 Then Exit Function
If NbBits > 32 Then Exit Function
If Decimale < 0 Then
Decimale = 2 ^ NbBits + Decimale
End If
If Decimale = 0 Or Decimale = 1 Then L = "" Else L = 1
Do
If Decimale Mod 2 = 0 Then B = "0" Else B = "1"
DecimalToBinary = DecimalToBinary & B
Decimale = Decimale \ 2
Loop Until Decimale <= 1
DecimalToBinary = StrReverse(DecimalToBinary & L)
If Completed = True Then
DecimalToBinary = Right("00000000000000000000000000000000" & DecimalToBinary, NbBits)
End If
End Function