|
-
Sep 18th, 2013, 03:50 AM
#1
[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!
-
Sep 18th, 2013, 04:37 AM
#2
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:
Public Function IsBitSet(data As Byte, index As Integer) As Boolean If EnsureValidIndex(index) Then Dim mask = GetBitMask(index) Return (data And mask) = mask End If End Function Public Sub SetBit(ByRef data As Byte, index As Integer) If EnsureValidIndex(index) Then data = data Or GetBitMask(index) End If End Sub Public Sub ClearBit(ByRef data As Byte, index As Integer) If EnsureValidIndex(index) Then data = data And Not GetBitMask(index) End If End Sub Public Sub ToggleBit(ByRef data As Byte, index As Integer) If EnsureValidIndex(index) Then data = data Xor GetBitMask(index) End If End Sub Private Function EnsureValidIndex(index As Integer) As Boolean If index < 0 OrElse index > 7 Then Throw New ArgumentOutOfRangeException("index", index, "Value must be in the range 0 to 7 inclusive.") End If Return True End Function Private Function GetBitMask(index As Integer) As Byte Return CByte(2 ^ index) End Function
-
Sep 18th, 2013, 04:45 AM
#3
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!
-
Sep 18th, 2013, 05:10 AM
#4
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.
-
Sep 18th, 2013, 05:12 AM
#5
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!
-
Sep 18th, 2013, 08:11 AM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|