Reading a binary field

J

John Wright

I have a device hooked up to my COM port that I read data from the serial
port into my program. If the device is in its double mode the information
that comes back is an 8 bit binary field for each record on the machine.
Set up as follows

Bit 7 -- Data type (Set to a 1)
Bit 6 -- Not used (set to 0)
Bits 5 - 0 -- Set to a 1 if the corresponding field is checked on the
machine.

So there are six fields on the machine, if they are checked on the machine I
will get the following:

Field E is marked on the machine:
Field5 Field4 Field3 Field2 Field1 Field0
0 0 0 0 1 0

So I would get from the machine 100000010 or 0x82

Fields A, B and E
Field5 Field4 Field3 Field2 Field1 Field0
1 1 0 0 1 0

So I would get from the machine 10110010 or 0xB2

No columns marked
Field5 Field4 Field3 Field2 Field1 Field0
0 0 0 0 0 0

I would get from the machine 100000000 or 0x80

All Fields marked
Field5 Field4 Field3 Field2 Field1 Field0
1 1 1 1 1 1

So I would get 1011111111 or 0xBF

Etc.

What I need to do is take this value and convert it to a string so I can get
the ones from the binary field and match the selected values from bits 5 to
0 to match on the GUI. Anyone have a clue?

Thanks.

John
 
G

Guest

John,

Assuming you get the data into a variable of type Byte, you can convert that
byte value into a string of ones and zeroes with Convert.ToString.

For example, if you have the input in a byte variable named myByte, to
convert it into a string of ones and zeroes:

Convert.ToString(myByte, 2)

Kerry Moorman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top