Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1437
  • Last Modified:

Pulling data from an 8 bit Byte in vb.net

I am trying to read a byte that returns different states from a byte. In slot 3 I have a bit to return either a 0x41 or 0x51.  in slot 4 I could have a state of 0x49 or 0x059. I use a SerialCOM.ReadExisting right now. How can I read those bit positions and return values?
0
cmdolcet
Asked:
cmdolcet
  • 8
  • 7
  • 2
  • +1
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Do you mean you are using the bit value to determine if your value is 0x41 or 0x51? A single bit can only be 1 or 0, so can't actually store the values you mentioned.

So do you actually just want to check if a bit at a given position is set or not?
0
 
cmdolcetAuthor Commented:
sorry yes I would like to check the given position if it is set or not.
0
 
Carl TawnSystems and Integration DeveloperCommented:
In that case you need to do a binary AND with the position you want to check:
if ((your_byte & (1 << your_position)) != 0)
{
     // bit is set
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cmdolcetAuthor Commented:
how to I read a string value into a byte?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also use the BitArray() class like this:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim B As Byte = 8 ' (4th slot on, 3rd slot off)
        Dim bits As New BitArray(New Byte() {B})

        ' Remember that indices are zero based in .Net:
        If bits.Item(2) Then
            MessageBox.Show("3rd bit is ON")
        Else
            MessageBox.Show("3rd bit is OFF")
        End If

        If bits.Item(3) Then
            MessageBox.Show("4th bit is ON")
        Else
            MessageBox.Show("4th bit is OFF")
        End If
    End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you give examples of your string values?
0
 
cmdolcetAuthor Commented:
the string value returns a "0"
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you give more details on that last response?...not sure what that means in the context of the big picture.
0
 
cmdolcetAuthor Commented:
OK well I have a command to my device that returns a value of "0" this value of "0" is the last bit returned in position (7) of the Byte.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So you are getting a value back from the device, and it's in the form of a string.

I think you're asking how to pass the string to BitArray and check the last bit?
        Dim response As String = "128"
        Dim bits As New BitArray(New Byte() {CByte(response)})
        If bits.Item(7) Then
            MessageBox.Show("Last bit is ON")
        Else
            MessageBox.Show("Last bit is OFF")
        End If

Open in new window

0
 
cmdolcetAuthor Commented:
would the same hold true if I needed to check the 3 bit or 4th bit in the byte?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Yes.  Just change the 7 in this line to correspond to the bit you're interested in:
If bits.Item(7) Then

Open in new window

*Remembering that they are zero based indices.
0
 
cmdolcetAuthor Commented:
Yes, however not I think I am royally confused. WhenI initially asked the question it was to see if a bit was 1 or 0 now from running the code below I am not needing that at all. I still need to check the bits (3), (4),(7) however I am suppose to be looking for "A" or 0x41 HEX in bit (3) or another value "I" or 0x49 Hex

is that another conversion function that needs to be preformed?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Well...each bit can only be 1 (one) or 0 (zero) so they can't represent "A" or "I".

The entire byte, however, can represent an "A" or "I", since a byte can old the values 0 (zero) thru 255:

0x41 HEX = 65 DEC = "A" ASCII
0x49 HEX = 73 DEC = "I" ASCII

What are you actually getting back from the device, though?  Are you getting a single byte value?...or are you getting a series of bytes that represent the string representation for value, in hex notation?

There is no "standard" way to do this.  Does the device have any documentation for the response that you can post?
0
 
cmdolcetAuthor Commented:
I am actually actually only returning a "0" or "1" right now. I send the device a command and I get the response as "0" or "1" this was to return the state. What was explained was that in the byte there would be placed in bit position 3 a hex value of 0x41 or 0x51 in position 4 bit a value of 0x49 or 0x59. It was told that I look for the hex value in position 3 and position 4 of the byte. Of course position 7 always returns the value 0 or 1.

I hope this helps
0
 
David Johnson, CD, MVPOwnerCommented:
As has been stated many times before a bit is either a 0 or a 1 nothing else.. You are mixing up the 0x0a  and the ascii character A ( 0x41).  You cannot look for a byte in a bit since a byte is 8 bits.
0
 
cmdolcetAuthor Commented:
OK, so I am confused I should just be reading in the byte. Which should be representing the following:

0x41 HEX = 65 DEC = "A" ASCII
0x49 HEX = 73 DEC = "I" ASCII

So what I really need is to read the byte and look for the DEC value "A" or "I"
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
My best guess is to use code like I originally posted:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28369847.html#a39873935

I think the values you are looking for are not actually stored in the byte, but merely represented by the bits.  For example, if bit 3 is off then you interpret it as "A", otherwise interpret as "I" (or it could be the other way around).
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 8
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now