parsing the byte stream....

This is a discussion on parsing the byte stream.... within the C Programming forums, part of the General Programming Boards category; Hello, I receive a payload which a byte stream: e.g. Code: typedef unsigned char uint8; typedef unsigned int uin16; //Following ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    parsing the byte stream....

    Hello,

    I receive a payload which a byte stream:

    e.g.
    Code:
    typedef unsigned char uint8;
    typedef unsigned int uin16;
    
    //Following is the payload:
    
    uint8 data[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x08,0x09,0x11}
    
    //now i need to parse them to fill the structure defined below
    
    typedef struct
    {
       uint16  a;
       uint8    b;
       char     c;
       uint8    d;
       uint16   e;
    }test;
    besides parsing each byte and filling the structure members.

    Is there any other way to directly fill the structure as a whole?.
    Code ex: will help.

    I tried doin this....but the device is throws exception:

    Code:
    test * parsed;
    uint8 *u8EPt = data;
    
    memcpy(parsed,(test  *)u8EPt,sizeof(test ));
    Thanks
    Last edited by sanddune008; 11-18-2009 at 11:06 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'parsed' doesn't actually point anywhere useful. You can't just copy to random pointers and expect it to work. You could always try a union.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    And how i do that?

    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which part? Search for dynamic allocation (hint: malloc), or union (covered probably close to where you learned about structures), in the search engine of your choice.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There is also something called structure padding. Which is tricky in these cases because you won't know the size of the structure. Google it for more information.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Thanks.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 04:19 AM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM
  5. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 10:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21