Page 368 - Programming Microcontrollers in C
P. 368

Numeric Encoding     353

                        {
                          if(*sp==’0') /* handle a zero input */
                                 *sp=’0'+0xa;
                          *pq|=(*sp-’0')<<4 ;
                        }
                        else
                          pq|=0xf0; /* non digit, mark it */
                        if(isdigit(*(sp+1))) /* the next input */
                        {
                          if(*sp==’0')  /* treat a zero */
                                 *sp=’0'+0xa;
                          *pq|=(*(sp+1)-’0');
                        }
                        else
                          pq|=0xf; /* another non digit */
                        sp+=2; /* Increment the input data pointer, */
                        pq++;  /* the output data pointer, */
                        i++;        /* and the data count */
                       }
                       return i; /* length of the array */
                   }
                              Listing 7-1: Numeric Encoding Routine

                              All of the storage arrays in the EEPROM are of the type un­
                          signed int. Here the storage of both numeric data and alpha
                          data requires that every bit of every storage location be used so that
                          unsigned is the norm. In the coding routines, the data are passed in as
                          characters and the destination arrays are unsigned. Therefore, to aid
                          the local bookkeeping, the destination array pointer is immediately
                          assigned to a type char * and this pointer will be used to store the
                          encoded data.
                              The following program provides a simple test for the numeric
                          coding. This program has a serious problem though. All computers
                          configure memory in either a big endian or a little endian order. In
                          the big endian order, the most significant byte, 8 bits, of a 16-bit
                          address is given the smaller address and the least significant byte
                          goes to the larger address. The little endian order is just the reverse.
   363   364   365   366   367   368   369   370   371   372   373