Page 379 - Programming Microcontrollers in C
P. 379

364    Chapter 7  Advanced Topics

                          to zero and bit is given the value bitbase. Then the code for
                          each character is retrieved successively. If the character read is a
                          space, the first entry in the table is used; if it is a new line character,
                          the second entry in the table is used; and if it is any other letter, the
                          letter is converted to an index into the alphabet and that particular
                          code, offset by two, is used as the code string for the input character.
                   static char *code[]={
                   “100”,“101”,”0001",”0000111",”000010",”111110",
                   ”01",“0000110110”,”000011000",”000001",”1100",
                   ”000011001",“1111111”,”1110",”111100",”1101",
                   ”0011",”111111001",“11111101111”,”0010",”111101",
                   ”000000",”111111000",“111111010”,”0000110111",
                   ”11111101110",”000011010",“1111110110”
                   };


                   #include <ctype.h>
                   int encode(char *a,unsigned *array,int length)
                   {
                       unsigned i,bit,bitbase=~(~0u>>1);
                       int c=1;
                       char *ptr,*pa;

                       pa=a;
                       for(i=0;i<length;i++)
                        array[i]=0;           /* initialize the array */
                       i=0;
                       bit=bitbase;
                       while(c!=’\n’)
                       {
                        c=*pa++; /* assumes file is not empty */
                        if(isalpha(c=toupper(c))||c==’ ‘||c==’\n’)
                        {
                          if(c==’ ‘)
                                 ptr=code[0];
                          else if(c==’\n’)
                                 ptr=code[1];
                          else
                                 ptr=code[c-’A’+2];
   374   375   376   377   378   379   380   381   382   383   384