Thread: Reading a “short” variable into a structure

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    Reading a “short” variable into a structure

    Hi,

    I have a sequence of 4 bytes that I wish to read into a structure.

    The structure definition is

    Code:
    struct A
    {
       u_char Char1;
       u_char Char2;
       u_short Short3;
    }
    Then I wish to compare A.Short3 with a 2-byte value, i.e.

    Code:
    #define Match 0x0800
    struct A *H;
    u_char *p;  // points to a sequence of bytes 12 34 08 00
    
    H = (struct A *)(p);
    if (H->Short3 == Match)
       ...
    However, I seem to be unable to read the 3rd and 4th byte as a short variable. I printed out the hex value of H->Short3 and Match, and got 8 and 800 respectively. It seems like H->Short3 only got the byte 0x08 instead of both bytes 0x0800.

    How do I read 0x0800 into H->Short3?

    Thank you!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    How are you attempting to read the short variable? What data are you reading by your method?

    Without those bits of information, there is no way to answer your question.

    My guess is that you have a padding problem - for performance reasons, compilers often add padding between struct members. Your code is assuming there is no such padding.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Verify that sizeof(struct A) is indeed 4

    2. Verify that this gives you the same result.
    Code:
    struct A H;
    u_char *p;  // points to a sequence of bytes 12 34 08 00
    memcpy(&H,p,sizeof(H));
    if (H.Short3 == Match)
    If p is a mis-aligned address, then dereferencing members with alignment requirements can produce weird effects.

    3. Verify the endian of your machine.
    Code:
    struct A H;
    H.Char1 = 0x12;
    H.Char2 = 0x34;
    H.Short3 = Match;
    unsigned char *p = (unsigned char *)&H;
    for ( i = 0 ; i < 4 ; i++ ) printf("%02x ", p[i]);
    If your machine is opposite endian to your data stream, then you'll see
    12 34 00 08
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It looks like order problem

    I would read 2 bytes into short using code:
    Code:
    H->Short3 = (u_short)high;
    H->Short3 <<= 8;
    H->Short3 |= (u_short)low;
    looks like in your method the values of high and low are swapped
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    Thank you all for the suggestions, I'll try them out and get back with the results. BTW, the sequence of bytes I'm reading from is from a pcap handle, i.e. I'm reading packet data.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so the byte order is defined by rfc describing corresponding header you are reading - use it.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    Thanks to everybody for your suggestions. It was indeed an endian issue and after correcting for it, I'm able to read the bytes in the correct order.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-19-2013, 11:30 PM
  2. Reading/Writing to a file error? (short code)
    By tmac619619 in forum C Programming
    Replies: 5
    Last Post: 10-15-2012, 07:00 PM
  3. Replies: 5
    Last Post: 10-01-2012, 10:57 PM
  4. what does a structure variable mean?
    By vsriharsha in forum C Programming
    Replies: 9
    Last Post: 08-26-2004, 10:52 PM
  5. What cannot be done with a structure variable?
    By correlcj in forum C Programming
    Replies: 8
    Last Post: 08-05-2002, 01:19 AM