Thread: help with c program: binary to ascii program

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

    help with c program: binary to ascii program

    This is a program where i input a 8 digit binary character and it calculates binary to ascii so if i enter a binary character 01000001 the program calculate and give me the ascii 65 (which is a 'A')

    My problem is, once i get 8 character i want to calculate the 8 and get the ascii then calculate the next set. Right now it calculate the last set i input.

    ie. i enter 1010101011110000, itll do the calculations for 11110000 and not the first part which is 10101010

    would i have to add code into the if statement?
    Code:
    int bit_to_ascii(const char core[ ], char data[ ]);
    
    int main( ) {
       char core2[256];
       char data2[13];
       int rc2;
       printf("enter: ");
       scanf("%256[^\n]", core2);
       rc2 = bit_to_ascii(core2, data2);
       printf("%d", rc2);
       return 0;
    }
    
    int bit_to_ascii(const char core[ ], char data[ ]) {
       int i, rc = 0, j, inte[7];
       char binarray[9];
       for(i=0, j=0; core[i] != '\0'; i++) {
         switch(core[i]) {
    	 case '1':
    	 case '0':
      	  if(j<8) {
    	       binarray[j++] = core[i];
    	    }
    	    else {
                   j = 0; /* resets j to 0 after 8 1's or 0's have been found */
              if(j<8) {
    	       binarray[j++] = core[i]; 
    	    }
        }
    	    break;
          }
       }
       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;
       
       printf("the binary array is: '%s' %d %d %d\n", binarray, inte[0], inte[1], inte[2]);
       return rc;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, when you reset j to 0, you then go ahead and write over the eight bits you had just read in. So at the end of the day, you've only got the last eight bits in your array. At that point, you should maybe stop reading and write out an answer, or store an answer, or do something with the data before reading more in.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    oh what a simple mistake thanks alot man i got it working now

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i have a next one though, i forgot to mention but if the last set is less than 8 characters i need to add 0's to make it a full 8 characters

    so if the last set is: 10101

    then i need to add 3 0's so it'll be 10101000

    this is what i tried. i got the lenght of the binarray string, and the subtract the lenght by 8 (lenght is 6, do 8 - 6 equals 2 that means i add 2 zero's) but i can't get it working

    Code:
       len = strlen(binarray);
       printf("&#37;d", len);
       if(len<8){
                 len2 = 8-5;
                 printf("%d", len);
                 }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Before or after you set binarray[j]='\0'?

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    right now its like this (after binarray = /0)

    Code:
    binarray[j] = '\0';
       len = strlen(binarray);
       printf("&#37;d\n", len);
                    if(len<8){
                              len2 = 8 - len;
                              printf("%d\n", len2);
                              for(m=len2; m<9; m++){
                                          binarray[len2]=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;

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why not try
    Code:
    binarray[len2] = '0';

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    with single quotes like ' it does the same thing like before, with double quote it says this "61 D:\Untitled1.cpp invalid conversion from `const char*' to `char' "

    also i made a mistake it should say

    Code:
    binarray[m] = '0';
    not len2 where it says m

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you want the single quotes. So now we're back to "not working". Does len print what you think it should? And I don't think you have your loop correct; if you've read in 6 characters, so that len is 6, then your for-loop will zero out from 2 to 8. You should just go from len to 7 with the '0's.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    you are right again i got it working good now, ill show you what i did for the loop

    Code:
    binarray[j] = '\0';
       len = strlen(binarray);
       printf("&#37;d\n", len);
                    if(len<8){
                              len2 = 8 - len;
                              m = len;
                              printf("%d\n", len2);
                              /*for(m=len2; m<9; m++){*/
                              do {
                                          binarray[m]='0';
                                          m++;
                              }while (m != 8);            
                                          /*}*/
                              }

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i got a new problem i just need a way to start coding this but while searching for the 1's and 0's i need to also look for the word 'RTP' and the word 'RTP' is followed by a number. Any suggestions on how to do this because when i find RTP, the number after it determines how my times the 8 1's/0's are used (ie. if i find RTP4, then the 8 1's/0's are lets say 10101010 then i have to make it output 4 times;
    10101010
    10101010
    10101010
    10101010)

    this is my code right now
    Code:
    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;
       char binarray[8];
       
       for(i=0, j=0; core[i] != '\0'; i++) {
         switch(core[i]) {
    	 case '1':
    	 case '0':
         if(j<8) {
    	       binarray[j++] = core[i];
    	    }
    	    
         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;
       
             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
           
          }//end switch
          
         }//end for loop
         
       //for last set of 8 1's/0's  
       binarray[j] = '\0';
       
       len = strlen(binarray); //finds lenght to determine how much zero's to add if lenght is less than 8
       printf("%d\n", len);
                    if(len<8){
                              len2 = 8 - len;
                              m = len;
                              printf("%d\n", len2);
                              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;
       
       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]);
       return rc;
    }
    Last edited by bigmac(rexdale); 01-14-2008 at 12:22 PM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your indentation is such a mess that's it's impossible (read: too much effort) to work out what code is meant to be run in association which each control structure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    how is it now??

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you've got more cases to work with now, since there are more characters that you care about. I would put 'R' in the switch, and in that case look to see if the next two characters are T and P.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    does it matter the placement, like if i put a case 'R': before the case 0: and 1:?

    also, how should i check if the next two are TP? this is where im stuck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert to ASCII program almost working
    By ggraz in forum C Programming
    Replies: 8
    Last Post: 09-17-2008, 06:54 PM
  2. binary search program
    By galmca in forum C Programming
    Replies: 5
    Last Post: 01-25-2005, 12:00 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. ASCII -> Binary
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-13-2001, 05:51 AM
  5. Replies: 4
    Last Post: 10-16-2001, 02:00 PM