Solved

Pulling data from an 8 bit Byte in vb.net

Posted on 2014-02-20
18
1,088 Views
Last Modified: 2014-02-25
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
Comment
Question by:cmdolcet
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39873833
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
 

Author Comment

by:cmdolcet
ID: 39873865
sorry yes I would like to check the given position if it is set or not.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39873881
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
 

Author Comment

by:cmdolcet
ID: 39873929
how to I read a string value into a byte?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 39873935
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39873940
Can you give examples of your string values?
0
 

Author Comment

by:cmdolcet
ID: 39874053
the string value returns a "0"
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39874096
Can you give more details on that last response?...not sure what that means in the context of the big picture.
0
 

Author Comment

by:cmdolcet
ID: 39874376
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39874492
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
 

Author Comment

by:cmdolcet
ID: 39874806
would the same hold true if I needed to check the 3 bit or 4th bit in the byte?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39874895
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
 

Author Comment

by:cmdolcet
ID: 39875014
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39875116
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
 

Author Comment

by:cmdolcet
ID: 39876622
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
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39878783
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
 

Author Comment

by:cmdolcet
ID: 39882392
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39882421
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now