Extracting certain bits from sequence of bits

This is a discussion on Extracting certain bits from sequence of bits within the C Programming forums, part of the General Programming Boards category; OK so I have a char* variable, which contains the header for a particular packet. There are various fields in ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    Extracting certain bits from sequence of bits

    OK so I have a char* variable, which contains the header for a particular packet. There are various fields in this packet but the header isn't split up into different variables. So my question is, how do I extract a hex number from this sequence of bits?

    i.e. if header = 02C0001400F00000
    and I need header[4-8] = 0014
    so I can convert it into decimal 20 using strtol

    Thanks for any help!

    -Luke

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Point to the start of your long with the first parameter to strtol.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    But wouldn't that convert the entire string after that element? I need it to stop at header[8]. And also how do you point to an element within an array? Because if I just put header[4] as the first argument, it would obviously only convert the one bit.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I really don't care to always come up with special-purpose examples when that is within the grasp of the OP, but if you are willing to indulge me and wait a bit I'll edit one in.

    [edit]My bad. It's late.
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       const unsigned char buffer[] = {0x02,0xC0,0x00,0x14,0x00,0xF0,0x00,0x00};
       unsigned short value = (buffer[2] << 8) | buffer[3];
       printf("value = &#37;hu\n", value);
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    ummm, well I'm willing to wait, but I'll work it out myself if you don't want to, was just trying to save a bit of time...

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Thank you It's a big help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 11:52 AM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. sequence
    By braddy in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:15 PM
  4. wsprintf and format specifiers
    By incognito in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 09:00 PM
  5. Replies: 4
    Last Post: 07-05-2002, 08:03 AM

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