Function ConvertDecToBin(DecVal As Currency) As String Dim r As Long Dim sValeur As String Dim sResult As String Dim HexVal As Variant HexVal = Array("0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", _ "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111") sValeur = Hex(DecVal) sValeur = Right$("00000000" & sValeur, 8) For r = 8 To 1 Step -1 sResult = HexVal(Val("&h" & (Mid$(sValeur, r, 1)))) & sResult Next r ConvertDecToBin = Format(sResult, "0000 0000 0000 0000 0000 0000 0000 0000") End Function
Range("C3").Value = ConvertDecToBin(Range("B3").Value)
Option Explicit ' IEEE 754 single precision binary floating-point format ' Définition des flottants 32 bits en simple précision : ' 0 01111100 01000100000001100000000 ' | \____/ \___________________/ ' Sign Exponent Significand ' 1 bit 8 bits 23 bits ' Lecture des bits de gauche à droite = format Little Endian Public Type LARGE_INTEGER LowPart As Long HighPart As Long End Type Public Function LargeIntegerToDouble(Value As LARGE_INTEGER) As Double Dim sBits As String Dim lSignBit As Long ' 1 bit Dim sExponent As String ' 8 bits Dim lExponent As Long Dim sSignificand As String ' 23, puis 24 bits Dim dSignificand As Double Dim dCoeff As Double Dim r As Long Dim sTemp As String ' Cas particuliers sTemp = Right$("0000" & Hex(Value.HighPart), 4) & _ Right$("0000" & Hex(Value.LowPart), 4) If sTemp = "00000000" Or _ sTemp = "80000000" Then ' +0 et -0 LargeIntegerToDouble = 0 ElseIf sTemp = "7F800000" Then ' + Infini LargeIntegerToDouble = 3.402823E+38 ElseIf sTemp = "FF800000" Then ' - Infini LargeIntegerToDouble = -1.401298E-45 ElseIf sTemp = "3EAAAAAB" Then ' 1/3 LargeIntegerToDouble = CDbl(1 / 3) Else ' Conversion du LongInteger en bits, plus facile à découper que Hexa sBits = LargeIntegerToBits(Value) ' Extraction des bits lSignBit = CLng(Mid$(sBits, 1, 1)) sExponent = Mid$(sBits, 2, 8) sSignificand = "1" & Mid$(sBits, 10, 23) lExponent = BitsToLong(sExponent) - 127 dCoeff = 1 For r = 1 To 24 If Mid$(sSignificand, r, 1) = "1" Then dSignificand = dSignificand + dCoeff End If dCoeff = dCoeff / 2 Next r LargeIntegerToDouble = ((-1) ^ lSignBit) * (dSignificand * (2 ^ lExponent)) End If End Function Private Function HexToBits(Value As String) As String Dim sTemp As String sTemp = Right$(String$(8, "0") & Value, 8) HexToBits = LongToBits(CLng("&h" & Left$(sTemp, 4))) + _ LongToBits(CLng("&h" & Right$(sTemp, 4))) End Function Private Function LargeIntegerToBits(Value As LARGE_INTEGER) As String LargeIntegerToBits = LongToBits(Value.HighPart) & LongToBits(Value.LowPart) End Function Private Function LongToBits(Value As Long) As String ' Version 16 bits Dim r As Long Dim sTemp As String For r = 0 To 15 If (Value And CLng(2 ^ r)) = CLng(2 ^ r) Then sTemp = "1" & sTemp Else sTemp = "0" & sTemp End If Next r LongToBits = sTemp End Function Private Function BitsToLargeInteger(Bits As String) As LARGE_INTEGER ' Renvoie une valeur 32 bits représentée par les Bits Dim sTemp As String sTemp = Right$(String$(32, "0") & Bits, 32) BitsToLargeInteger.HighPart = BitsToLong(Left$(sTemp, 16)) BitsToLargeInteger.LowPart = BitsToLong(Right$(sTemp, 16)) End Function Private Function BitsToLong(Value As String) As Long ' Version 16 bits Dim r As Long Dim ltemp As Long For r = 1 To Len(Value) ltemp = ltemp + (CLng(Mid$(Value, r, 1)) * (2 ^ (Len(Value) - r))) Next r BitsToLong = ltemp End Function
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question