Hallo,
ich meine Funktion, die einen Ausschnitt eines Byte-Arrays als Long zurückgeben soll, funktioniert nur bis zu einer Länge von 15 Bits und dabei nur, wenn von dem 1. Bit aus gestartet wird.
BytePos und BitPos markieren den Beginn (beginnend mit 1 für das erste Bit/Byte):
Private Function ByteArrayToDecAtPos(ByRef byteArray() As Byte, ByVal BytePos As Long, ByVal BitPos As Integer, ByVal CountBits) As Long
Dim sizeOfByteArr As Long
sizeOfByteArr = (UBound(byteArray) - LBound(byteArray) + 1)
'dynamic input parameter check
If BytePos < 0 Or BytePos > sizeOfByteArr Or CountBits > sizeOfByteArr * 8 Then
ByteArrayToDecAtPos = CVErr(xlErrValue)
Exit Function
Else
'implementation of function
ByteArrayToDecAtPos = 0
curByte = BytePos
'process all bytes except the last one
For i = 1 To ((CountBits - (BitPos - 1) + 7) / 8) - 1
temp = shr(byteArray(i - 1), (BitPos - 1))
If i < ((CountBits - BitPos + 15) / 8) And (BitPos > 1) Then
temp2 = shr(byteArray(i), (8 - (BitPos - 1)))
temp2 = shl(temp2, (BitPos - 1))
End If
ByteArrayToDecAtPos = shl(ByteArrayToDecAtPos, 1) + temp
Next i
'process the last byte
temp = shr(byteArray(((CountBits - (BitPos - 1) + 7) / 8) - 1), (8 - ((CountBits + (BitPos - 1)) Mod 8)) Mod 8)
ByteArrayToDecAtPos = shl(ByteArrayToDecAtPos, ((CountBits + (BitPos - 1)) Mod 8)) + temp
End If
End Function
Ich vermute, da fehlt noch etwas.
shr und shl sind die bitwise-shifting Funktionen:
Public Function shl(ByVal Value As Long, ByVal Shift As Byte) As Long
shl = Value
If Shift > 0 Then
Dim i As Byte
Dim m As Long
For i = 1 To Shift
m = shl And &H40000000
shl = (shl And &H3FFFFFFF) * 2
If m <> 0 Then
shl = shl Or &H80000000
End If
Next i
Else
shl = Value
End If
End Function
Public Function shr(ByVal Value As Long, ByVal Shift As Byte) As Long
Dim i As Byte
shr = Value
If Shift > 0 Then
shr = Int(shr / (2 ^ Shift))
Else
shr = Value
End If
End Function
Wer hilft mir, die Funktion "ByteArrayToDecAtPos" entsprechend zu korrigieren?
Vielen Dank im Voraus.
|