1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

bit manipulation

Discussion in 'C' started by answerme, Apr 20, 2010.

  1. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    I have 13 bytes of data which i need to parse
    First 5 bytes are char
    next 2 bytes are int
    next 3 bytes are char
    nxt 1 bye is char
    remaining 2 byte is int

    How should i do bit manipulation
     
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    one way is this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
        int i;
        char bytes[14]="ABCDE12ABC%56";
        char b1[6]="";
        char b2[3]="";int b2i;
        char b3[4]="";
        char b4;
        char b5[3]="";int b5i;
            for (i=0;i<5;i++){//5 chars
               b1[i]=bytes[i];
            }
            for (i=5;i<7;i++){//integer
               b2[i-5]=bytes[i];
            }
            b2i=atoi(b2);
             for (i=7;i<10;i++){//3 chars
                b3[i-7]=bytes[i];
             }
             b4=bytes[10];//char
             for (i=11;i<13;i++){//integer
                b5[i-11]=bytes[i];
             }
             b5i=atoi(b5);
        printf("\n string before parsing=<%s>",bytes);
        printf("\n first=<%s>",b1);
        printf("\n second=<%d>",b2i);
        printf("\n third=<%s>",b3);
        printf("\n fourth=<%c>",b4);
        printf("\n fifth=<%d>",b5i);
        getchar();
        return 0;
    }
    
    
     

Share This Page