Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Pulling data from an 8 bit Byte in vb.net

Posted on 2014-02-20
Medium Priority
1,376 Views
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
Question by:cmdolcet
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 7
• 2
• +1

LVL 52

Expert Comment

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

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

LVL 52

Expert Comment

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
}
``````
0

Author Comment

ID: 39873929
how to I read a string value into a byte?
0

LVL 86

Accepted Solution

Mike Tomlinson earned 1600 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
``````
0

LVL 86

Expert Comment

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

Author Comment

ID: 39874053
the string value returns a "0"
0

LVL 86

Expert Comment

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

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

LVL 86

Expert Comment

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
``````
0

Author Comment

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

LVL 86

Expert Comment

ID: 39874895
Yes.  Just change the 7 in this line to correspond to the bit you're interested in:
``````If bits.Item(7) Then
``````
*Remembering that they are zero based indices.
0

Author Comment

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 86

Expert Comment

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

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 83

Expert Comment

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

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 86

Expert Comment

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the claâ€¦
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a feâ€¦
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on tâ€¦
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompaâ€¦
###### Suggested Courses
Course of the Month5 days, 6 hours left to enroll