Detecting byte pattern

This is a discussion on Detecting byte pattern within the C Programming forums, part of the General Programming Boards category; Hello, Could you tell me what is wrong with this code: Code: unsigned char data[4]={0x00,0x00,0x01,0xb3}; //byte pattern int i = ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    26

    Detecting byte pattern

    Hello,

    Could you tell me what is wrong with this code:

    Code:
    unsigned char data[4]={0x00,0x00,0x01,0xb3}; //byte pattern
     int i = 0;
    while(m->data_len > 100) {
     
        if (m->payload[i] != 0x00) {   
            i++;
                                     } //skipping through bytes until I find 0x00
        else {
        if (memcmp(&m->payload[i],data,sizeof(data)) == 0) {
                           printf("I frame");
    
    break;                                                                      }
    
       
             }
                    }
    Full code: [C] Code - Pastebin.com

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Hard to tell what the code is supposed to do.
    One mistake could be that you do not advance i any more when 0x00 is found.
    Kurt

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Quote Originally Posted by ZuK View Post
    Hard to tell what the code is supposed to do.
    One mistake could be that you do not advance i any more when 0x00 is found.
    Kurt
    The main idea is to detect wanted bytes pattern. I must be doing smt wrong here. I skip through bytes until I find 0x00 and then with memcmp() function try to check if wanted pattern starts here. If not it should search further. Maybe my code search only for first 0x00 and then jumps to another packet?

    This code works well If I know exact place of bytes pattern.

    MC

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by MiniComa View Post
    Could you tell me what is wrong with this code:
    What did you expect? What error messages you get?

    Code:
    unsigned char data[4]={0x00,0x00,0x01,0xb3}; //byte pattern
     int i = 0;
    while(m->data_len > 100)
     {
        if (m->payload[i] != 0x00) 
        {   
            i++;
        } //skipping through bytes until I find 0x00
        else
        {
            if (memcmp(&m->payload[i],data,sizeof(data)) == 0)
            {
                printf("I frame");
                break;                                                                      
            }
        }
    }
    You never change m->data_len inside your while-loop thus you loop infinitely if m->data_len is greater than 100 and never find the pattern you want.
    And you should learn to indent your code properly.

    Bye, Andreas
    Last edited by AndiPersti; 08-12-2012 at 07:14 AM. Reason: add important detail

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Then you should also increment i when a 0x00 was found but memcmp() fails.
    Kurt

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Quote Originally Posted by ZuK View Post
    Then you should also increment i when a 0x00 was found but memcmp() fails.
    Kurt
    Thank you. Cant believe I was so dumb to miss it.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Quote Originally Posted by AndiPersti View Post
    What did you expect? What error messages you get?

    Code:
    unsigned char data[4]={0x00,0x00,0x01,0xb3}; //byte pattern
     int i = 0;
    while(m->data_len > 100)
     {
        if (m->payload[i] != 0x00) 
        {   
            i++;
        } //skipping through bytes until I find 0x00
        else
        {
            if (memcmp(&m->payload[i],data,sizeof(data)) == 0)
            {
                printf("I frame");
                break;                                                                      
            }
        }
    }
    You never change m->data_len inside your while-loop thus you loop infinitely if m->data_len is greater than 100 and never find the pattern you want.
    And you should learn to indent your code properly.

    Bye, Andreas
    Thank you Andreas. I am newbie in programming so all info are very useful to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Comparision byte by byte
    By anusha2489 in forum C Programming
    Replies: 12
    Last Post: 05-16-2011, 07:58 AM
  2. reading files byte by byte
    By cpsc in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2011, 03:54 PM
  3. TIFF IFD byte by byte
    By ghostcoder in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2010, 04:33 PM
  4. C++ Pattern Help
    By Lestat in forum C++ Programming
    Replies: 10
    Last Post: 10-24-2007, 03:05 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM

Tags for this Thread


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