Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 finding some digits in an array problem
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: Sat Jan 26, 2008 11:56 am   Post subject: finding some digits in an array problem

I need to read through an array and take 8 digits out of it if they are either 0 or 1, when i got 8 digits i just convert it into ascii. My problem is that at one point in the array theres only 6 digits so it keeps running to the next bit of digits in the array. how would i add 2 more 0's for example for those 6 digits. might be situations with 5 4 or 3 digits as well so i need to add 0 to the end.


function - Ignore the case R its for the RPT part.
code:


int bit_to_ascii(const char core[ ], char data[ ])

{
int i,ii,track=0,sum=0,total=128;
int t,num=0;
int asum;
char value;

int a;


    for (i=0,ii=0;core[i] != '\0';i++)
        {
       
         switch(core[i])
         {
          case '0':
          case '1':
               sum += total * (core[i] - '0');
               total /= 2;
               ii++;
               asum = sum;
               printf("%d\n",sum);
                   
                    if(ii == 8)
                    {
                     if (sum > 31 && sum < 127)
                        {     
                               data[track] = sum;
                               track++;
                               
                               
                        }
                     
                      sum = 0;
                      total = 128;
                      ii = 0; 
                               
                    }           
                 
          case 'R':
                           
               if (core[i+1] == 'P' && core[i+2] =='T')
                  {
                   if (core[i+3] > '0' && core[i+3] <= '2')
                      num += (core[i+3] -'0');
                   if (core[i+4] >= '0' && core[i+4] <= '9')
                   {
                      num=0;           
                      num += ((core[i+3] -'0') * 10 ) + (core[i+4] -'0');
                   }   
                   if (core[i+5] >= '0' && core[i+5] <= '9')
                   {
                      num=0;           
                      num += ((core[i+3] -'0') * 100 ) + ((core[i+4] -'0') * 10) + (core[i+5] -'0') ;   
                   }                         
                 
                       
                  // printf("%d  %d\n",num,asum);
                   
                  if (sum>31 && sum<127 && ii != 8)
                  data[track] = asum;
                 
                  asum=0;
                  value = data[track];
                 
                  for (t=0;t<num;t++)
                   {
                   data[track+t] = value;
                  // printf("%c\n",value);
                   }
                   num = 0;                     
                  }           
               
                 
               
             
          }   
         
       
                                 
                     
         
                         
        }
       //  for (a=0;a<6;a++){
       // printf("%c\n",data[a]);}                   

return track + 1;
}




Sample Main

code:


int main( ) {
   const char core2[ ] =
      "$% FrJ9) 0 + -<, 1 4Sx 0 <- L 01 M? 0 @ 0 0 Ux#"  /* 01001000 =  72 = 'H'  */
      "r5 0 #! 1 1 gP 0 f66-:] 0 [ } 1 v4t^ 0 1 n>?:77"  /* 01100101 = 101 = 'e'  */
      " junk ^*@#^% 0110  more junk }{r;,<> 1100 RPT22 "  /* 01101100 = 108 = 'l'  */
      " junk ^*@#^% 0110  more junk }{r;,<> 11 RPT2 "  /* 01101100 = 108 = 'l'  */
      "0  ^jh  1 ]{) BM8 1 - 0 _! 9 + 1  118Y pz(x pM1"  /* 01101111 = 111 = 'o'  */;

   char data2[13];
   int rc2;

   rc2 = bit_to_ascii(core2, data2);
   getchar();
   return 0;
}                 
[/quote]
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: