473,472 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Newbie question: How to define a class that will work on bits from abinary file?

Hi all,
Newbie question here wrt defining a class that will work on bits read
from a binary file. How would you go about doing it? As an example
please look at the structure of my data given below. The data comes in
40 byte packets via stdin or a binary file.
my_Data_pkt(){
syncByte (8bits)
XML_type (2bits)
XML_subtype (2bits)
record_value (3bits)
playout_flag (1bit)
if (playout_flag=='1') {
playout_length (8bits)
for (i=0; i< playout_length; i++){
playout_data
}
}
payload to fill the rest of the 40 bytes
}
How would this be defined as a class?
How would you retrieve the values for the different variables within
the packet?
How would you set the variables to a specific value?

Any help would be appreciated.

Regards
Jun 27 '08 #1
2 1502
Damfino wrote:
Hi all,
Newbie question here wrt defining a class that will work on bits read
from a binary file. How would you go about doing it? As an example
please look at the structure of my data given below. The data comes in
40 byte packets via stdin or a binary file.
my_Data_pkt(){
syncByte (8bits)
XML_type (2bits)
XML_subtype (2bits)
record_value (3bits)
playout_flag (1bit)
if (playout_flag=='1') {
playout_length (8bits)
for (i=0; i< playout_length; i++){
playout_data
}
}
payload to fill the rest of the 40 bytes
}
How would this be defined as a class?
// assuming that 'char' is 8 bits

class DataPacket {
// somehow control the alignment and make it 1 byte
// to avoid padding between members of this class
char syncByte; // not sure you need this to be kept
struct BitStuff {
unsigned XML_type:2;
unsigned XML_subtype:2;
unsigned record_value:3;
unsigned payout:1;
} fields;
char restOfPacket[38];
public:
// member functions go here
};
How would you retrieve the values for the different variables within
the packet?
Not sure what retrieval you're talking about.
How would you set the variables to a specific value?
By initialising or assigning them.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On 15 May, 10:35, James Kanze <james.ka...@gmail.comwrote:
On May 15, 9:14 am, Michael DOUBEZ <michael.dou...@free.frwrote:
Victor Bazarov a écrit :
Damfino wrote:
>Newbie question here wrt defining a class that will work on
>bits read from a binary file. How would you go about doing
>it? As an example please look at the structure of my data
>given below. The data comes in 40 byte packets via stdin or
>a binary file.
>my_Data_pkt(){
>*syncByte * (8bits)
>*XML_type (2bits)
>*XML_subtype *(2bits)
>*record_value *(3bits)
>*playout_flag (1bit)
>*if (playout_flag=='1') {
>* * playout_length (8bits)
>* * for (i=0; i< playout_length; i++){
>* * * playout_data
>* * }
>*}
>*payload to fill the rest of the 40 bytes
>}
>How would this be defined as a class?
// assuming that 'char' is 8 bits
And assuming the machine is in little endian. In big endian,
you would have to invert the bit fields.

Or maybe it wouldn't work at all. *How the compiler lays out bit
fields is implementation dependent, and varies greatly. *Bit
fields cannot be used for mapping external data formats.
yes
It's possible to write a stream which reads arbitrary bit
lengths;
I was part way though trying to implement something like
this.

To the OP:
I assumed the data had already been loaded into an array
(or std::vector) of Bytes (unsigned chars). And the class
operated on the blocks of data.

Something like:

typedef std::vector<BytePacket;

class BitStream
{
public:
BitStream (Packet&);

// assumes n <= 8
void getBits (const Byte&, int n) const;
void putBits (Byte&, int n);

// used when n 8
void getBits (const Packet&, int n) const;
void putBits (Packet&, int n);

private:
Packet byte_stream_;
size_t byte_index_;
size_t bit_index_;
};

then's theres a lot of shifts and "ands" and "ors"
I did it once in the past (for use with a compression
algorithm). *It's rarely necessary, however, since in practice,
files aren't defined as streams of bits, but as streams of
bytes. *
ah. If only this were so... There are still bit
oriented protocols out there. Not all the world is
TCP/IP.

So his actual file format is probably something more
like:

* * * * * * * *+---+---+---+---+---+---+---+---+
* * byte 1 * * | * * * * * * *sync * * * * * * |
* * * * * * * *+---+---+---+---+---+---+---+---+
* * * * * * * *+---+---+---+---+---+---+---+---+
* * byte 2 * * | type *|subtype| rec. value| F |
* * * * * * * *+---+---+---+---+---+---+---+---+

* * *...

He streams in bytes (as unsigned char), and processes them one
after the other.
<snip>

--
Nick Keighley
Jun 27 '08 #3

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

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
2
by: Bryan Parkoff | last post by:
I create one 32 Bits variable and four pointer variables. Four pointer variables link or point to one 32 Bits variable. Each pointer variable is 8 Bits. Look at my example below. unsigned int...
5
by: Stephen Mayes | last post by:
Please humor a curious hobbyist. Below is some code I made to amuse myself and reverse bit order. Any comments or corrections are welcome, but I have some specific concerns. I added the #define...
10
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
3
by: Christian Christmann | last post by:
Hi, in my application I often need constant values depending on their bitwidth. For this purpose I wrote some static functions that look like this: #include <cmath> const int...
8
by: Fred | last post by:
I've got following program encapsuled fscanf, however, it doesn't work. I'm sure that the content format in "a.txt" is OK, the content would be correctly read if using fscanf directly, what's...
0
nev
by: nev | last post by:
I know when is best to use some templates but listed below are most that I don't know when is best to use or even know how to use: I am creating a windows application with mysql backend. 1....
20
by: Plissken.s | last post by:
Is there an efficient way to create a struct of flag in C++? I need to create a struct of boolean flag, like this: struct testStruct { bool flag1; bool flag2; bool flag3; bool flag4; bool...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.