Help: Bin to ASCII program

This is a discussion on Help: Bin to ASCII program within the C Programming forums, part of the General Programming Boards category; what the program does is search through the string core looking for 1's and 0's and every 8 1's and ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Help: Bin to ASCII program

    what the program does is search through the string core looking for 1's and 0's and every 8 1's and 0's it converts it to decimal (01010000 is 80 as a decimal, so 80 in the string variable data is a 'P')

    but also it looks for RPT with a number after RPT (ie. RPT2) the number means how much times to repeat the binary (ie. 01010000 RPT2 means there will be two 'P's because 01010000 is a 80 a decimal 80 is 'P' in ascii)

    then there is SKP, which when found skips the next 8 binary characters (ie. 01010000 SKP 00110000 means 00110000 will not get calculated)

    well i got the code down for all of this but i think i have some logic error because when i put 01001111RPT00201010000 i get 01001111 two time because RPT002 but i dont get 01010000. If someone check just chekc out my code and tell me what i should add or take out or modify that would be nice.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int bit_to_ascii(const char core[ ], char data[ ]);
    
    int main( ) {
       char core2[256];
       char data2[13];
       int rc2;
       printf("enter: ");
       scanf("&#37;256[^\n]", core2);
       rc2 = bit_to_ascii(core2, data2);
       return 0;
    }
    
    int bit_to_ascii(const char core[ ], char data[ ]) {
       int i, rc = 0, j, inte[7], len, len2, m, lenrpt, rpt1, rptcounter, bin, lendata = 0, p = 0;
       char binarray[9], rpt[3], joe[256];
       for(i=0, j=0; core[i] != '\0'; i++) {//loops until end of string
         switch(core[i]) {
    	 case '1':
    	 case '0':
      	  if(j<8) {
    	       binarray[j++] = core[i];//puts 1 or 0 in the variable binarray
    	    }
          else {
             binarray[j] = '\0';  
             inte[0] = (binarray[0] - 48) * 128;
             inte[1] = (binarray[1] - 48) * 64;
             inte[2] = (binarray[2] - 48) * 32;
             inte[3] = (binarray[3] - 48) * 16;
             inte[4] = (binarray[4] - 48) * 8;
             inte[5] = (binarray[5] - 48) * 4;
             inte[6] = (binarray[6] - 48) * 2;
             inte[7] = (binarray[7] - 48) * 1;
             bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];//calculates binarray to decimal
             data[lendata] = bin;
             lendata++;
       
             printf("the binary array is: '%s' %d %d %d %d %d %d %d %d\n", binarray, inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
           j = 0; /* resets j to 0 after 8 1's or 0's have been found */
           if(j<8) {
    	       binarray[j++] = core[i]; 
    	    }//end if
        }//end else
        case 'R':
           if(core[i+1] == 'P' && core[i+2] == 'T') {//checks if the next 2 characters are PT to make RPT
              if(core[i+3]>='0' || core[i+3]<='9') {
                 rpt[0] = core[i+3];//get number after RPT
                  
                    if(core[i+4]>='0' || core[i+4]<='9') { 
                       rpt[1] = core[i+4];
                          
                          if(core[i+5]>='0' || core[i+5]<='9') {
                             rpt[2] = core[i+5]; 
                          }
                    } 
                 lenrpt = strlen(rpt);//gets lenght of number after rpt
                 i = i + lenrpt; 
                 rpt[3] = '\0'; 
                 rpt1 = atoi (rpt);//uses string rpt to put in rpt1 as a integer
                 for(rptcounter=1; rptcounter!=rpt1; rptcounter++){
                    len = strlen(binarray);
                    if(len<8){
                              len2 = 8 - len;
                              m = len;
                              do {
                                          binarray[m]='0';
                                          m++;
                              }while (m != 8);            
                              }
                    inte[0] = (binarray[0] - 48) * 128;
                    inte[1] = (binarray[1] - 48) * 64;
                    inte[2] = (binarray[2] - 48) * 32;
                    inte[3] = (binarray[3] - 48) * 16;
                    inte[4] = (binarray[4] - 48) * 8;
                    inte[5] = (binarray[5] - 48) * 4;
                    inte[6] = (binarray[6] - 48) * 2;
                    inte[7] = (binarray[7] - 48) * 1;
                    bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
                    data[lendata] = bin;
                    lendata++;
       
                    printf("the binary array is: '%s' %d %d %d %d %d %d %d %d\n", binarray, inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
                 }                                
              }   
           }
        break;
        case 'S':
           if(core[i+1] == 'K' && core[i+2] == 'P') {//check for KP to make SKP
           i = i + 3;
                 do {//loops 8 times to skip 8 1's/0's            
                    switch(core[i]) {
    	              case '1':
    	              case '0': 
                      p++;
                      i++;
                      break;
                    }   
                 } while (p<7);         
                                     
           }
        break;     
          }//end case
          }//end for
       binarray[j] = '\0';
       len = strlen(binarray);
                    if(len<8){
                              len2 = 8 - len;
                              m = len;
                              do {
                                          binarray[m]='0';
                                          m++;
                              }while (m != 8);            
                              }
       inte[0] = (binarray[0] - 48) * 128;
       inte[1] = (binarray[1] - 48) * 64;
       inte[2] = (binarray[2] - 48) * 32;
       inte[3] = (binarray[3] - 48) * 16;
       inte[4] = (binarray[4] - 48) * 8;
       inte[5] = (binarray[5] - 48) * 4;
       inte[6] = (binarray[6] - 48) * 2;
       inte[7] = (binarray[7] - 48) * 1;
       bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
       data[lendata] = bin;
       lendata++;
       data[lendata]='\0';
          
       printf("the binary array is: '%s' %d %d %d %d %d %d %d %d\n", binarray, inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
       
       printf("\nBit Converted to ASCII is: %s\n", data);//display the ascii
       rc = strlen(data);//find the lenght of the cahr data
       return rc;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't like this line one little bit:
    Code:
                lenrpt = strlen(rpt);//gets lenght of number after rpt
    especially just after I watched you put three characters in a 3-character array. And then after that:
    Code:
                 rpt[3] = '\0';
    Tsk tsk.

    If you want to use that strlen, make sure (1) you have enough space, and (2) you have \0 filled in your array already, so that you can find the end of the string later.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    should i up the rpt index by 1? (ie. rpt[4])

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That would take care of (1). You would still need to do (2).

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    rpt[4] = '\0' after declaring it?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bigmac(rexdale) View Post
    rpt[4] = '\0' after declaring it?
    Best thing would be to do
    Code:
    rpt[4] = {0};
    as that guarantees that the whole array is filled with zeros. Just bear in mind that if you attempt to re-use the same variable, you'll also need to re-fill the string, something like:
    Code:
    memset(rpt, 0, sizeof(rpt));
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ok guys i found out the problem

    i = i + lenrpt;

    ^^it had no propose so i decided to comment it out and the program worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  3. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM
  4. Results for the Encryption Contest -- June 23, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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