views:

52

answers:

3

Ex.

typedef struct 
{
  bool streamValid;
  dword dateTime;
  dword timeStamp;
  stream_data[800];
} RadioDataA;

Ex. Where stream_data[800] contains:

**Variable**  **Length (in bits)**
packetID        8
packetL         8
versionMajor    4
versionMinor    4
radioID         8

etc..

I need to write:

void unpackData(radioDataA *streamData, MA_DataA *maData)
{
  //unpack streamData (from above) & put some of the data into maData
  //How do I read in bits of data? I know it's by groups of 8 but I don't understand how.
  //MAData is also a struct.
}
+1  A: 

I'm not sure I understood it right, but why can't you do just:

memcpy(maData, streamData->stream_data, sizeof(MA_DataA));

This will fully copy data contained in the array of bytes to the structure.

Max
`struct` s may be laid out with padding in between members.
caf
A: 

I'm just trying to unpack data and output it. I'm just stuck on how to work with bits and keeping an index and determining how to truncate it into my different variables.

The stream_data[800] is of type byte. Sorry!!

I don't think memcopy will work because it's not 1:1 direct transfer.

hope you get what I mean!

Chelp
Please, find a 'edit' button under your original question, and add this whole 'answer' into your original question. Afterward, you can delete this comment with delete button. Thanks in advance.
Max
I can't! there's no edit button. Wonder if it's because I posted this question from a different computer i.e IP address. I think Judge Maygarden gave me what I was looking for! Thanks Maygarden! :O) /Chelp
Chelp
+1  A: 

Your types are inconsistent or unspecified. I believe you are trying to extract packed data from a byte stream. If so, assume buf contains your data packed in order with the lengths specified. The following code should then extract each field correctly:

int packetID = buf[0];
int packetL = buf[1];
int versionMajor = (buf[2] >> 4);
int versionMinor = (buf[2] & 0x0F);
int radioID = buf[3];

As you can see, the byte-aligned values are straightforward copies. However, the 4-bit fields must be masked and/or shifted to extract only the desired data. For more information on bitwise operations refer to the excellent Bit Twiddling Hacks code snippets.

Judge Maygarden