Option
Explicit
Sub
test()
Dim
values(5)
As
Byte
values(0) = 25
values(1) = 50
values(2) = 100
values(3) = 150
values(4) = 200
values(5) = 255
Debug.Print get_Bitsequenz(values(), 2, 3, 15)
End
Sub
Public
Function
get_Bitsequenz(
ByRef
values()
As
Byte
, startByte
As
Integer
, startBit
As
Byte
, anzahlBits
As
Byte
)
As
String
If
startByte > UBound(values())
Then
MsgBox
"'startByte' in Array nicht vorhanden."
, vbExclamation
Exit
Function
End
If
Dim
sValues()
As
String
Dim
i
As
Integer
Dim
s
As
String
ReDim
sValues(UBound(values()))
For
i = 0
To
UBound(values())
sValues(i) = ByteToBit(values(i))
Next
i
If
Len(sValues(startByte)) < startBit
Then
MsgBox
"'startBit' ist größer als die Bitlänge."
, vbExclamation
Exit
Function
End
If
i = startByte
Do
While
Len(s) < anzahlBits
And
i <= UBound(sValues)
If
i = startByte
Then
s = s & Mid(sValues(i), startBit + 1, Len(sValues(i)) - startBit)
Else
s = s & sValues(i)
End
If
i = i + 1
Loop
If
Len(s) < anzahlBits
Then
MsgBox
"Zu wenig Bits vorhanden."
, vbExclamation
Exit
Function
End
If
get_Bitsequenz = Left(s, anzahlBits)
End
Function
Private
Function
ByteToBit(
ByVal
v
As
Byte
)
As
String
Dim
s
As
String
, e
As
Byte
, i
As
Integer
e = v
Do
While
Not
e = 0
s = s &
CStr
(e
Mod
2)
e =
CByte
(Int(e / 2))
Loop
For
i = 1
To
8 - Len(s)
s = s &
"0"
Next
i
ByteToBit = StrReverse(s)
End
Function