473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading a binary field

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
Jul 6 '07 #1
1 1566
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.ToStrin g.

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

Convert.ToStrin g(myByte, 2)

Kerry Moorman
"John Wright" wrote:
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
Jul 7 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3395
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be created when the program executes. But the compile keeps reading that it can't find the text file. Karl you wrote the original program from which this one is but a poor copy, can you or anyone else enlighten me as to why yours worked and mine...
7
6054
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
1
1504
by: RML | last post by:
Hi, My VS 2005 VB app reads a binary file that was created by a VS 2005 VC++ WinCE app. The VC++ app writes the following structure to the file. typedef struct { short ID; TCHAR Num; } PARTS_STRUCT;
6
5259
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
16
3737
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have two questions.. 1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1, sizeof(struct bmp), 1, fin); i have a Segment violation ??
3
1735
by: katz911 | last post by:
Hello, I've encountered a strange problem which I can't seem to explain. I've written a simple C++ class that deals with a certian binary file. In one of the methods there, I wish to update a file - namely to read a certain field whose offset I know, and then write to another field, whose offset I also know. In order to read the data, I first use file.seekg(offset, ios::beg), and then a read command; I've checked the variable, and...
6
11898
by: John Wright | last post by:
I am trying to read the data from a device on a serial port. I connect just fine and can receive data fine in text mode but not in binary mode. In text mode the data from the device comes in like this: S~5BBBBBBBBBBBBBBB5BBBBBBBB5B31BB4BB2B5BB4BBBE S is the start of the line, ~ indicates a good read, B is a blank reading, and the numbers 1-6 correspond to a location on the device for a line. So there are six positions on the line...
6
3521
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
2
1843
by: RoddyPiper | last post by:
I am working with a sql server database. Due to a database udgrade, sever fields which were integer are now binary(16). These fields are used for joins later in the VB project. The problem I am haveing is that I have no idea how to read in the binary field! Here is the original code(using Visual Studio 08): strRptPars(iCount).lID = adoOleDbDataReader.GetInt32(i) Originally strRptPars(iCount).lID was defined as LONG. I tried changing it to...
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8631
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.