tags:

views:

57

answers:

3

I hope the title is describing the problem, i'll change it if anyone has a better idea.

I'm storing information in a struct like this:

struct AnyStruct
{
    AnyStruct() :
        testInt(20),
        testDouble(100.01),
        testBool1(true),
        testBool2(false),
        testBool3(true),
        testChar('x') {}

    int testInt;
    double testDouble;
    bool testBool1;
    bool testBool2;
    bool testBool3;
    char testChar;

    std::vector<char> getBinaryBlock()
    {
        //how to build that?
    }
}

The struct should be sent via network in a binary byte-buffer with the following structure:

Bit 00- 31: testInt
Bit 32- 61: testDouble most significant portion
Bit 62- 93: testDouble least significant portion
Bit     94: testBool1
Bit     95: testBool2
Bit     96: testBool3
Bit 97-104: testChar

According to this definition the resulting std::vector should have a size of 13 bytes (char == byte)

My question now is how I can form such a packet out of the different datatypes I've got. I've already read through a lot of pages and found datatypes like std::bitset or boost::dynamic_bitset, but neither seems to solve my problem.

I think it is easy to see, that the above code is just an example, the original standard is far more complex and contains more different datatypes. Solving the above example should solve my problems with the complex structures too i think.

One last point: The problem should be solved just by using standard, portable language-features of C++ like STL or Boost (

+1  A: 

I think all you need is described in this FAQ: http://www.parashift.com/c++-faq-lite/serialization.html

zerm
As far as I can see, this is about Serialization in general. I do understand how datatypes could be serialized. The problem is to convert the given datatypes into a binary format and add them to the right position in a binary container.Then the next question is how i could generate a std::string or a std::vector<char> out of them.
MOnsDaR
A: 

Basically you would allocate an array of unsigned char (BYTEs) of the length of the total size of all variables in your class (use sizeof() ). Then you would copy the contents of each variable on the correct offset using memcpy.

Note that this is just a basic approach and I would definetly suggest to take a look at the c++ faq link in zerm's answer.

PeterK
This does not solve the problem because memcpy could just copies entire Bytes. E.g. it is not possible copy the value of 8 bools into a char to the right positions.Or did I understand that wrong?
MOnsDaR
You didn't, but your assumption that the size of bool is just one bit is wrong. Try sizeof(bool) and you will see that it is 1, which means 1 byte.
PeterK
Yes but a bool could be stored into a bit, this way it is specified in the standard I have to use. (as given in the above example where the value of the 3 bools should be stored in the bits 94-96)I could not achieve this by using memcpy as far as i understand its functionality.
MOnsDaR
With some bit shifting and bit logical operations, it might be possible.
PeterK
A: 

I've now used the std::bitset for generating bit-sequences for the datatypes and then concatenated these bitsets to one big bitset. This then holds the data I need. I just have to check for little/big endian.

MOnsDaR