Problem regarding binary to char conversion

This is a discussion on Problem regarding binary to char conversion within the C Programming forums, part of the General Programming Boards category; I'm basically coding a packet sniffer and no, it's not for any evil intent. Basically, it's supposed to read a ...

  1. #1
    LuP
    LuP is offline
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Question Problem regarding binary to char conversion

    I'm basically coding a packet sniffer and no, it's not for any evil intent. Basically, it's supposed to read a packet like this one:

    0
    10000010000000000000011000101101111110111110000011 0000010000000000000101000011000001

    Now the first line represents the message number and the rest of the packet represents the actual message. The beginning and end of each message portion is denoted by the 7 bit string of 1000001. The actual message above is "Boo!" in case you're wondering.

    The problem is, it doesn't seem to actually take in the initial 1000001, and it loops infinitely while displaying a converted 'A'.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    FILE* inFile;
    char packetNum;
    char* chunk;
    char* inside;
    long int convert;
    
    int main(int argc, char* argv[]){
      clrscr();
      if (argc < 2){
        printf("Insufficient parameters.\n");
        printf("\nConvert <filename>\n");
        exit(1);
      }
    
      inFile = fopen(argv[1], "r");
      if (inFile == NULL) {
        printf("Unable to open %s.  Quitting now.\n", argv[1]);
        exit(1);
      } else printf("File Name: %s\n", argv[1]);
      
      packetNum = fgetc(inFile); 
      /*above line takes in the message number from the first line of src*/
      printf("Looking for message#: %c\n", packetNum);
      chunk = "1000001"; /*had to force it for some reason*/
      while (fgets(chunk, 8, inFile) == "1000001"){ 
        /*if it gets 1000001, it starts reading inside via this loop until 
    it hits 1000001 again*/
        while (fgets(inside, 8, inFile) != "1000001"){
          /*convert binary string into char and display it*/
          convert = strtol(inside, (char**)NULL, 10);
          printf("Test: ");
          printf("%c\n", convert); 
        }  
        printf("-l\n"); /* represents the end of the decoded word */
        }  
      fclose(inFile);
      printf("Done.\n"); 
      return(0);
    }
    Can anyone tell me what went wrong? Any help would be appreciated.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Firts of all, you need to allocate space for chunk and inside.
    You are reading 8 characters at the time so you need space for at least 9 characters (8 + null character). Second, when you want to compare 2 strings use strcmp .
    If a function returns a value, always check it for errors.

    Code:
    char chunk[9];
    char inside[9];
    
    while( (fgets(chunk, 8, inFile) != NULL) && (strcmp(chunk, "10000001") == 0) )
    {
      /* do something */
    }
    Cheers,
    Monster

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  3. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

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