Solved

Pulling data from an 8 bit Byte in vb.net

Posted on 2014-02-20
18
1,243 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
[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
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

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

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 86

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 86

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
 
LVL 86

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 86

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 86

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 81

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 86

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net dbnull syntax 1 58
SSIS Column mapping 5 40
vb.net code 9 28
Delete command SQLdatasource in asp.net, vb 2 21
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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