-1

Language : C++

I am working on Bit Packing (Extracting the required bits from the given data and packing them in a char*) . My code currently supports : - Integers - Characters - Strings

Now if I have to store the required bits of a structure, how should I go about it ? I mean what should I expect as input parameters for a generalized code w.r.t structures ?

The question may be vague and I am not expecting direct answers, even hints and pointers are appreciated.

Naveen
  • 74,600
  • 47
  • 176
  • 233
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

3 Answers3

3

Have a look at this for a very packed format or use an standard marshalling format such as json, xml, boost serialization,... and save yourself the grey hair.

piotr
  • 5,657
  • 1
  • 35
  • 60
  • Thank you for the informative link. The option of using the external libraries will be considered at the end when I feel there is no other way to do it. – Aquarius_Girl Sep 30 '10 at 09:29
  • You shouldn't (wait until there is no other way). Except for learning how to do it there are very few reasons to write a library on your own, especially when you are new to the domain. So considering external libraries should be the first thing to do, and writing yourself should be the last option, especially for such complex basic components (Just IMVHO) – Fabio Fracassi Sep 30 '10 at 10:40
  • I just found **bitmagic** library: http://bmagic.sourceforge.net/ Can this be used w.r.t above problem ? Has it got any serious limitations ? – Aquarius_Girl Oct 01 '10 at 07:58
  • But what are you tring to do, bitsets as flags? or Marshalling data into a buffer? it's not clear from your question. If you just want bitset, use that container from the STL. – piotr Oct 01 '10 at 10:10
  • Piotr, Thanks for following up ! I actually **do not know** which one of the above 3, is *better* for packing structures and whether there is some standard algorithm to be used for bit packing ? I saw Huffman coding algorithm. but That's not related to this, is it ? I'll be greatful if some one can give some more pointers on all this. – Aquarius_Girl Oct 01 '10 at 10:24
  • This "packing structures" is called marshalling or serialization in computing jargon. Please use one of the two, because your questions are ambigous. Huffman coding is a technique for data compression. Could you explain futher what is that you are trying to do? Saving structures to disk for example? that is marshalling, and you'd probably be better to use boost-serialization for example. – piotr Oct 03 '10 at 22:09
0

As piotr already suggested: try using existing libraries to marshall.

However, since your already doing it yourself:
If your supported primitives are representable as bytes, then you shouldn't be bit packing (there might be some confusion about the term), otherwise consider using std::bitset.
Because C++ doesn't support reflection, there is no help in C++ to byte pack structures in a generic, safe and portable way, so be prepared to write a function per structure to pack each primitive and each member structure.
Overloading does help to call these functions in a generic way, so packing of containers (vector ...) can be done generically. However, if you want this, then prefer free functions over member functions, to avoid having a difference between packing primitives and packing structures.

Example:

void Pack(const MyStruct& str, Packer& pack)
{
  Pack(str.int1, pack);
  Pack(str.string1, pack);
  Pack(str.otherStruct, pack);
}  
stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • Thank you for the detailed response. I am not a native English speaker. Kindly explain what does "supported primitives" mean here. As you said : "so be prepared to write a function per structure to pack each primitive and each member structure." That's not feasible I IMO because this bit-packing function of mine is supposed to handle any type of structure not just one ! – Aquarius_Girl Sep 30 '10 at 09:30
  • and I am just checking what "bitset" does. Thanks for the information. – Aquarius_Girl Sep 30 '10 at 09:33
  • @anishakaul: You mentioned that your implementation already support integers, characters and strings. These are the supported primitives. Unsupported primitives include doubles, boolean, ... Structures and containers (and strictly speaking strings too) are compounds made up of primitives. – stefaanv Sep 30 '10 at 09:43
0

You can use reinterpret_cast<char*>() to access the struct as though it were a char*:

#include <iostream>
using namespace std;

struct myStruct
{
    char a; int b;
};

int main(int argc, char* argv[])
{
    myStruct instance = { 10, 100 };

    //Treat the myStruct instance as though it were a char*
    char* pchar = reinterpret_cast<char*>(&instance);

    //Output the values of the bytes to the console
    for(int i = 0; i < sizeof(instance); i++)
        cout << (int)pchar[i] << endl;

    getchar();
    return 0;
}

Note, however, that because of alignment, not all of the char's will contain meaningful data. This can be fixed, in visual studio at least, using the pack pragma.. but the results will still be highly architecture/compiler-dependent.

Thus, if you want to pack the bytes in a way that is at all portable/maintainable (ie. you're going to use this in the real-world), I would highly suggest using one of the serialization methods mentioned by piotr.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283