Results 1 to 6 of 6

Thread: [RESOLVED] How to handle bit-wise information recieved in an Byte-Array

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [RESOLVED] How to handle bit-wise information recieved in an Byte-Array

    Hi, I'm recieving data in an ByteArray.
    Most of the data is easily converted into numeric values, but I'm having problems handling those Bytes that hold information at Bit-Level.
    For example Items 3-4 of my ByteArray, which are 16 Bits are holding the information for 16 ON-OFF Type Properties.

    I tried something like:
    Code:
    Dim BitInfo(15) As BitArray
    Buffer.BlockCopy(ByteArray, 3, BitInfo, 0, 2) 'Raising error 	-2147024809 Object has to be an Array
    Code:
    Dim BitInfo As new  BitArray(4)
    Buffer.BlockCopy(ByteArray, 3, BitInfo, 0, 2) 'Doesn't compile because an BitArray can't be converted into an Array
    Is the BitArray the best way, or should I stay with the Bytes and try to read (and later write) each Bit form there?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to handle bit-wise information recieved in an Byte-Array

    Here are some methods that may be of use to you:
    vb.net Code:
    1. Public Function IsBitSet(data As Byte, index As Integer) As Boolean
    2.     If EnsureValidIndex(index) Then
    3.         Dim mask = GetBitMask(index)
    4.  
    5.         Return (data And mask) = mask
    6.     End If
    7. End Function
    8.  
    9. Public Sub SetBit(ByRef data As Byte, index As Integer)
    10.     If EnsureValidIndex(index) Then
    11.         data = data Or GetBitMask(index)
    12.     End If
    13. End Sub
    14.  
    15. Public Sub ClearBit(ByRef data As Byte, index As Integer)
    16.     If EnsureValidIndex(index) Then
    17.         data = data And Not GetBitMask(index)
    18.     End If
    19. End Sub
    20.  
    21. Public Sub ToggleBit(ByRef data As Byte, index As Integer)
    22.     If EnsureValidIndex(index) Then
    23.         data = data Xor GetBitMask(index)
    24.     End If
    25. End Sub
    26.  
    27. Private Function EnsureValidIndex(index As Integer) As Boolean
    28.     If index < 0 OrElse index > 7 Then
    29.         Throw New ArgumentOutOfRangeException("index", index, "Value must be in the range 0 to 7 inclusive.")
    30.     End If
    31.  
    32.     Return True
    33. End Function
    34.  
    35. Private Function GetBitMask(index As Integer) As Byte
    36.     Return CByte(2 ^ index)
    37. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How to handle bit-wise information recieved in an Byte-Array

    Thanks for those.
    In other words, you would suggest just to keep the data in a Byte-Format and procecute the reading and writing using those functions/Subs?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to handle bit-wise information recieved in an Byte-Array

    I've looked at the BitArray class before and it's never really seemed to be as useful as it should be so yes, I would stick with raw Bytes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [RESOLVED] How to handle bit-wise information recieved in an Byte-Array

    Roger that, I'll be working with your code.
    Leaving into the depth of my code;-)

    Dankeschön
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] How to handle bit-wise information recieved in an Byte-Array

    If these on/off values have some kind of names I would use an Enum and modify the code JMC provided.

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim somebytes() As Byte = {3, 5, 7, 2, 11, 13, 17, 19, 23, 29}
            If IsBitSet(somebytes(3), namesofbits.byt3bit1) Then
                SetBit(somebytes(3), namesofbits.byt3bit7)
                Stop
            End If
            If IsBitSet(somebytes(4), namesofbits.byt4bit3) Then
                ClearBit(somebytes(4), namesofbits.byt4bit3)
                Stop
            End If
        End Sub
    
        'give meaningful names
        <Flags> _
        Enum namesofbits As Byte
            byt3bit0 = 1 << 0
            byt3bit1 = 1 << 1
            byt3bit2 = 1 << 2
            byt3bit3 = 1 << 3
            byt3bit4 = 1 << 4
            byt3bit5 = 1 << 5
            byt3bit6 = 1 << 6
            byt3bit7 = 1 << 7
    
            byt4bit0 = 1 << 0
            byt4bit1 = 1 << 1
            byt4bit2 = 1 << 2
            byt4bit3 = 1 << 3
            byt4bit4 = 1 << 4
            byt4bit5 = 1 << 5
            byt4bit6 = 1 << 6
            byt4bit7 = 1 << 7
        End Enum
    
        Public Function IsBitSet(data As Byte, whbit As namesofbits) As Boolean
            Return (data And whbit) = whbit
        End Function
    
        Public Sub SetBit(ByRef data As Byte, whbit As namesofbits)
            data = data Or whbit
        End Sub
    
        Public Sub ClearBit(ByRef data As Byte, whbit As namesofbits)
            data = data And Not whbit
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width