Bonjour.
J'ai lutter pour pouvoir convertir un buffer(Byte) en Bits avec la fontion BitConverter du fait de l'inversion des Bits du à littleendian.
J'ai donc du faire qq fonction pour remedier à ce petit probleme.
Si vous avez mieux,ou critique, je suis preneur.
Merci
Ps:Je debute en prog et cette fonction est destiner à lire des Bits bien precis dans un buffer Icmp
Source / Exemple :
Option Strict On
Option Explicit On
Option Compare Binary
Namespace Convert
Public Module Bin
Public Const BinByteMaxLength As Integer = 8
Public Const BinIntegerMaxLength As Integer = 31
Public Const BinLongMaxLength As Integer = 63
Public Function GetBin(ByVal Value As Byte) As Byte() 'ok
Dim BinOut(BinByteMaxLength - 1) As Byte
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(System.BitConverter.GetBytes(Value))
Dim i As Integer
For i = 0 To BinByteMaxLength - 1
BinOut(i) = System.Convert.ToByte(BitArray.Get(BinByteMaxLength - i - 1))
Next
GetBin = BinOut
BinOut = Nothing
BitArray = Nothing
i = Nothing
End Function
Public Function GetBin(ByVal Value As Byte, ByVal Index As Integer, ByVal Length As Integer) As Byte()
Dim BinOut(Length - 1) As Byte
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(System.BitConverter.GetBytes(Value))
Dim i, i2 As Integer
For i = Index To Index + Length - 1
BinOut(i2) = System.Convert.ToByte(BitArray.Get(BinByteMaxLength - i - 1))
i2 += 1
Next
GetBin = BinOut
BinOut = Nothing
BitArray = Nothing
i = Nothing
i2 = Nothing
End Function
Public Function GetBin(ByVal Value As Byte()) As Byte() 'ok
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(Value)
Dim BinOut(BitArray.Length - 1) As Byte
Dim i As Integer
For i = 0 To BitArray.Length - 1
BinOut(i) = System.Convert.ToByte(BitArray.Get(i))
Next
GetBin = BinOut
BinOut = Nothing
BitArray = Nothing
i = Nothing
End Function
Public Function GetBin(ByVal Value As Byte(), ByVal Index As Integer, ByVal Length As Integer) As Byte()
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(Value)
Dim BinOut(Length - 1) As Byte
Dim i, i2 As Integer
For i = Index To Index + Length - 1
BinOut(i2) = System.Convert.ToByte(BitArray.Get(i))
i2 += 1
Next
GetBin = BinOut
BinOut = Nothing
BitArray = Nothing
i = Nothing
i2 = Nothing
End Function
Public Function GetBin(ByVal Byte_1 As Byte, ByVal Byte_2 As Byte) As Byte() 'ok
Dim ByteArray() As Byte = {Byte_2, Byte_1}
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(ByteArray)
Dim BinOut(16 - 1) As Byte
Dim i As Integer
For i = 0 To BitArray.Length - 1
BinOut(i) = System.Convert.ToByte(BitArray.Get(16 - i - 1))
Next
GetBin = BinOut
ByteArray = Nothing
BinOut = Nothing
BitArray = Nothing
i = Nothing
End Function
Public Function GetBin(ByVal Byte_1 As Byte, ByVal Byte_2 As Byte, ByVal Index As Integer, ByVal Length As Integer) As Byte()
Dim ByteArray() As Byte = {Byte_2, Byte_1}
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(ByteArray)
Dim BinOut(Length - 1) As Byte
Dim i, i2 As Integer
For i = Index To Index + Length - 1
BinOut(i2) = System.Convert.ToByte(BitArray.Get(16 - i - 1))
i2 += 1
Next
GetBin = BinOut
ByteArray = Nothing
BinOut = Nothing
BitArray = Nothing
i = Nothing
i2 = Nothing
End Function
Public Function GetBin(ByVal Byte_1 As Byte, ByVal Byte_2 As Byte, ByVal Byte_3 As Byte) As Byte() 'ok
Dim ByteArray() As Byte = {Byte_3, Byte_2, Byte_1}
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(ByteArray)
Dim BinOut(24 - 1) As Byte
Dim i As Integer
For i = 0 To BitArray.Length - 1
BinOut(i) = System.Convert.ToByte(BitArray.Get(24 - i - 1))
Next
GetBin = BinOut
ByteArray = Nothing
BinOut = Nothing
BitArray = Nothing
i = Nothing
End Function
Public Function GetBin(ByVal Byte_1 As Byte, ByVal Byte_2 As Byte, ByVal Byte_3 As Byte, ByVal Index As Integer, ByVal Length As Integer) As Byte()
Dim ByteArray() As Byte = {Byte_3, Byte_2, Byte_1}
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(ByteArray)
Dim BinOut(Length - 1) As Byte
Dim i, i2 As Integer
For i = Index To Index + Length - 1
BinOut(i2) = System.Convert.ToByte(BitArray.Get(24 - i - 1))
i2 += 1
Next
GetBin = BinOut
ByteArray = Nothing
BinOut = Nothing
BitArray = Nothing
i = Nothing
i2 = Nothing
End Function
Public Function GetBin(ByVal Byte_1 As Byte, ByVal Byte_2 As Byte, ByVal Byte_3 As Byte, ByVal Byte_4 As Byte) As Byte() 'ok
Dim ByteArray() As Byte = {Byte_4, Byte_3, Byte_2, Byte_1}
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(ByteArray)
Dim BinOut(32 - 1) As Byte
Dim i As Integer
For i = 0 To BitArray.Length - 1
BinOut(i) = System.Convert.ToByte(BitArray.Get(32 - i - 1))
Next
GetBin = BinOut
ByteArray = Nothing
BinOut = Nothing
BitArray = Nothing
i = Nothing
End Function
Public Function GetBin(ByVal Byte_1 As Byte, ByVal Byte_2 As Byte, ByVal Byte_3 As Byte, ByVal Byte_4 As Byte, ByVal Index As Integer, ByVal Length As Integer) As Byte()
Dim ByteArray() As Byte = {Byte_4, Byte_3, Byte_2, Byte_1}
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(ByteArray)
Dim BinOut(Length - 1) As Byte
Dim i, i2 As Integer
For i = Index To Index + Length - 1
BinOut(i2) = System.Convert.ToByte(BitArray.Get(32 - i - 1))
i2 += 1
Next
GetBin = BinOut
ByteArray = Nothing
BinOut = Nothing
BitArray = Nothing
i = Nothing
i2 = Nothing
End Function
Public Function GetBin(ByVal Value As Integer) As Byte() 'ok
Dim BinOut(BinIntegerMaxLength - 1) As Byte
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(System.BitConverter.GetBytes(Value))
Dim i, Begin As Integer
Select Case Value
Case Is < &H100
Begin = 23
Case Is < &H10000
Begin = 15
Case Is < &H1000000
Begin = 7
Case Else
Begin = 0
End Select
For i = Begin To BinIntegerMaxLength - 1
BinOut(i) = System.Convert.ToByte(BitArray.Get(BinIntegerMaxLength - i - 1))
Next
GetBin = BinOut
BinOut = Nothing
BitArray = Nothing
i = Nothing
Begin = Nothing
End Function
Public Function GetBin(ByVal Value As Integer, ByVal Index As Integer, ByVal Length As Integer) As Byte() 'ok
Dim BinOut(Length - 1) As Byte
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(System.BitConverter.GetBytes(Value))
Dim i, i2, Begin As Integer
Select Case Value
Case Is < &H100
Begin = 23
Case Is < &H10000
Begin = 15
Case Is < &H1000000
Begin = 7
Case Else
Begin = 0
End Select
If Begin > Index Then
i2 = Begin - Index
Else
Begin = Index
End If
For i = Begin To Index + Length - 1
BinOut(i2) = System.Convert.ToByte(BitArray.Get(BinIntegerMaxLength - i - 1))
i2 += 1
Next
GetBin = BinOut
BinOut = Nothing
BitArray = Nothing
i = Nothing
i2 = Nothing
Begin = Nothing
End Function
Public Function GetBin(ByVal Value As Long) As Byte()
Dim BinOut(BinLongMaxLength - 1) As Byte
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(System.BitConverter.GetBytes(Value))
Dim i, Begin As Integer
Select Case Value
Case Is < &H100
Begin = 55
Case Is < &H10000
Begin = 47
Case Is < &H1000000
Begin = 39
Case Is < &H100000000
Begin = 31
Case Is < &H10000000000
Begin = 23
Case Is < &H1000000000000
Begin = 15
Case Is < &H100000000000000
Begin = 7
Case Else
Begin = 0
End Select
For i = Begin To BinLongMaxLength - 1
BinOut(i) = System.Convert.ToByte(BitArray.Get(BinLongMaxLength - i - 1))
Next
GetBin = BinOut
BinOut = Nothing
BitArray = Nothing
i = Nothing
Begin = Nothing
End Function
Public Function GetBin(ByVal Value As Long, ByVal Index As Integer, ByVal Length As Integer) As Byte()
Dim BinOut(Length - 1) As Byte
Dim BitArray As System.Collections.BitArray = New System.Collections.BitArray(System.BitConverter.GetBytes(Value))
Dim i, i2, Begin As Integer
Select Case Value
Case Is < &H100
Begin = 55
Case Is < &H10000
Begin = 47
Case Is < &H1000000
Begin = 39
Case Is < &H100000000
Begin = 31
Case Is < &H10000000000
Begin = 23
Case Is < &H1000000000000
Begin = 15
Case Is < &H100000000000000
Begin = 7
Case Else
Begin = 0
End Select
If Begin > Index Then
i2 = Begin - Index
Else
Begin = Index
End If
For i = Begin To Index + Length - 1
BinOut(i2) = System.Convert.ToByte(BitArray.Get(BinLongMaxLength - i - 1))
i2 += 1
Next
GetBin = BinOut
BinOut = Nothing
BitArray = Nothing
i = Nothing
i2 = Nothing
Begin = Nothing
End Function
Public Function BinToByte(ByVal Value() As Byte) As Byte
If Not Value Is Nothing Then
Dim Result As Byte
Dim i As Integer
For i = 0 To Value.Length - 1
If Value(Value.Length - i - 1) = 1 Then
Result += Value(Value.Length - i - 1) * CType(2 ^ i, Byte)
End If
Next
BinToByte = Result
Result = Nothing
i = Nothing
Else
BinToByte = Nothing
End If
End Function
Public Function BinToInteger(ByVal Value() As Byte) As Integer
If Not Value Is Nothing Then
Dim Result As Integer
Dim i As Integer
For i = 0 To Value.Length - 1
If Value(Value.Length - i - 1) = 1 Then
Result += Value(Value.Length - i - 1) * CType(2 ^ i, Integer)
End If
Next
BinToInteger = Result
Result = Nothing
i = Nothing
Else
BinToInteger = Nothing
End If
End Function
Public Function BinToLong(ByVal Value() As Byte) As Long
If Not Value Is Nothing Then
Dim Result As Long
Dim i As Integer
For i = 0 To Value.Length - 1
If Value(Value.Length - i - 1) = 1 Then
Result += Value(Value.Length - i - 1) * CType(2 ^ i, Long)
End If
Next
BinToLong = Result
Result = Nothing
i = Nothing
Else
BinToLong = Nothing
End If
End Function
Public Function BinToString(ByVal Value() As Byte) As String
If Not Value Is Nothing Then
Dim Result As New System.Text.StringBuilder
Dim i As Integer
Dim CultureInfo As System.Globalization.CultureInfo = CultureInfo.InvariantCulture
For i = 0 To Value.Length - 1
Result.Insert(Result.Length, Value(i).ToString(CultureInfo), 1)
Next
BinToString = Result.ToString
Result = Nothing
CultureInfo = Nothing
i = Nothing
Else
BinToString = Nothing
End If
End Function
End Module
End Namespace
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.