By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.