Page 370 - Programming Microcontrollers in C
P. 370

Numeric Decoding     355

                        if(isdigit(c=(*sa>>4)+’0'))
                          number[i++]=c;
                        else if(c==’0'+0xa)
                          number[i++]=’0';
                        if(isdigit(c=(*sa&0xf)+’0'))
                          number[i++]=c;
                        else if(c==’0'+0xa)
                          number[i++]=’0';
                        sa++;
                       }
                       number[i++]=’\n’;
                       number[i++]=0;
                   }
                              Listing 7-3: Numeric Decoding Routine
                              In this function the output data is called number[] and the in­
                          put is s[]. The encoded data in s[] is converted one BCD 4-bit
                          field at a time to ASCII characters. In the event that a character re­
                          ceived has a value ‘0’+0xa, it is then a character zero or ‘0’. Each
                          byte is converted from two 4-bit BCD values to two ASCII charac­
                          ters that represent the proper digits.
                              The test program for this routine is a combination of the encode
                          test routine with one to decode the encoded data. As one would ex­
                          pect, when both the encode and the decode routine are used together,
                          the result is correct. This observation is true on either the Intel or the
                          Motorola style chip. The endian-ness of the chip is immaterial when
                          the entire encode/decode operation is completed.

                   #include <stdio.h>
                   #define ARRAY_SIZE 100
                   int decode(unsigned M[],char *s);
                   int encode(char *a,unsigned *array,int length);


                   main()
                   {
                       char a[ARRAY_SIZE] ;
                       int c,i=0;
                       unsigned array[ARRAY_SIZE];
                       char s[ARRAY_SIZE];
   365   366   367   368   369   370   371   372   373   374   375