Thema Datum  Von Nutzer Rating
Antwort
Rot Bits aus einem Byte-Array lesen und als Long zurückgeben
20.06.2017 12:58:43 Stern77
NotSolved
20.06.2017 13:29:28 Gast24487
NotSolved
20.06.2017 14:35:05 Gast31647
NotSolved
20.06.2017 14:41:16 Stern77
NotSolved
20.06.2017 14:35:36 Stern77
NotSolved
20.06.2017 13:29:43 Gast16110
NotSolved
20.06.2017 22:39:34 Gast32854
NotSolved
21.06.2017 07:31:15 Stern77
NotSolved
21.06.2017 09:40:38 SJ
NotSolved
21.06.2017 10:16:56 Stern77
NotSolved
21.06.2017 10:24:08 Gast79558
NotSolved
21.06.2017 12:13:23 Stern77
NotSolved
21.06.2017 12:19:02 SJ
NotSolved
30.06.2017 14:48:57 Stern77
NotSolved
30.06.2017 16:16:45 Stern77
NotSolved

Ansicht des Beitrags:
Von:
Stern77
Datum:
20.06.2017 12:58:43
Views:
1297
Rating: Antwort:
  Ja
Thema:
Bits aus einem Byte-Array lesen und als Long zurückgeben

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.


Ihre Antwort
  • Bitte beschreiben Sie Ihr Problem möglichst ausführlich. (Wichtige Info z.B.: Office Version, Betriebssystem, Wo genau kommen Sie nicht weiter)
  • Bitte helfen Sie ebenfalls wenn Ihnen geholfen werden konnte und markieren Sie Ihre Anfrage als erledigt (Klick auf Häckchen)
  • Bei Crossposting, entsprechende Links auf andere Forenbeiträge beifügen / nachtragen
  • Codeschnipsel am besten über den Code-Button im Text-Editor einfügen
  • Die Angabe der Emailadresse ist freiwillig und wird nur verwendet, um Sie bei Antworten auf Ihren Beitrag zu benachrichtigen
Thema: Name: Email:



  • Bitte beschreiben Sie Ihr Problem möglichst ausführlich. (Wichtige Info z.B.: Office Version, Betriebssystem, Wo genau kommen Sie nicht weiter)
  • Bitte helfen Sie ebenfalls wenn Ihnen geholfen werden konnte und markieren Sie Ihre Anfrage als erledigt (Klick auf Häckchen)
  • Bei Crossposting, entsprechende Links auf andere Forenbeiträge beifügen / nachtragen
  • Codeschnipsel am besten über den Code-Button im Text-Editor einfügen
  • Die Angabe der Emailadresse ist freiwillig und wird nur verwendet, um Sie bei Antworten auf Ihren Beitrag zu benachrichtigen

Thema Datum  Von Nutzer Rating
Antwort
Rot Bits aus einem Byte-Array lesen und als Long zurückgeben
20.06.2017 12:58:43 Stern77
NotSolved
20.06.2017 13:29:28 Gast24487
NotSolved
20.06.2017 14:35:05 Gast31647
NotSolved
20.06.2017 14:41:16 Stern77
NotSolved
20.06.2017 14:35:36 Stern77
NotSolved
20.06.2017 13:29:43 Gast16110
NotSolved
20.06.2017 22:39:34 Gast32854
NotSolved
21.06.2017 07:31:15 Stern77
NotSolved
21.06.2017 09:40:38 SJ
NotSolved
21.06.2017 10:16:56 Stern77
NotSolved
21.06.2017 10:24:08 Gast79558
NotSolved
21.06.2017 12:13:23 Stern77
NotSolved
21.06.2017 12:19:02 SJ
NotSolved
30.06.2017 14:48:57 Stern77
NotSolved
30.06.2017 16:16:45 Stern77
NotSolved