Re: Newbie question: How to define a class that will work on bits
from a binary file?
On 15 May, 10:35, James Kanze <james.ka...@gmail.com> wrote:
On May 15, 9:14 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:
Victor Bazarov a =E9crit :
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<Byte> Packet;
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