decoding a binary data blob to a structure
<p>I have a Visual Studio 2008 C++ project where I need to decode a 6-byte array of data in to a structure.</p>

<p>My thought was to do something like this:</p>

<pre><code>#pragma pack( push, 1 )
struct Map 
{
    BYTE alpha    : 8;
    BYTE bravo    : 8;
    UINT charlie  : 24;
    BYTE delta    : 8;
    BYTE padding0 : 8;  // ensure correct byte boundary
    BYTE padding1 : 8;
};
#pragma pack( pop )
// sizeof( Map ) == 8 bytes

BYTE test_data[ 6 ] = { 0x07,     // alpha
                        0x0B,     // bravo
                        0x00,     // charlie high byte
                        0x00,     // charlie med byte
                        0x01,     // charlie low byte
                        0x33 };   // delta
Map map = { };
memcpy( &amp;map, test_data, sizeof( test_data ) );
ASSERT( map.alpha == 0x07 );
ASSERT( map.bravo == 0x0B );
ASSERT( map.charlie == 0x01 );
ASSERT( map.delta == 0x33 );
</code></pre>

<p>But, the data in <code>charlie</code> and <code>delta</code> is never correct. </p>

<p>Is there a way to structure my struct to get this correct or am I stuck setting each field individually?</p>

<p>Thanks</p>
