home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xntp3.zip / lib / hextolfp.c < prev    next >
C/C++ Source or Header  |  1992-03-10  |  1KB  |  70 lines

  1. /*
  2.  * hextolfp - convert an ascii hex string to an l_fp number
  3.  */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <strings.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10.  
  11. #include "ntp_fp.h"
  12.  
  13. int
  14. hextolfp(str, lfp)
  15.     char *str;
  16.     l_fp *lfp;
  17. {
  18.     register char *cp;
  19.     register char *cpstart;
  20.     register u_long dec_i;
  21.     register u_long dec_f;
  22.     char *ind;
  23.     static char *digits = "0123456789abcdefABCDEF";
  24.     extern char *index();
  25.  
  26.     dec_i = dec_f = 0;
  27.     cp = str;
  28.  
  29.     /*
  30.      * We understand numbers of the form:
  31.      *
  32.      * [spaces]8_hex_digits[.]8_hex_digits[spaces|\n|\0]
  33.      */
  34.     while (isspace(*cp))
  35.         cp++;
  36.     
  37.     cpstart = cp;
  38.     while (*cp != '\0' && (cp - cpstart) < 8 &&
  39.         (ind = index(digits, *cp)) != NULL) {
  40.         dec_i = dec_i << 4;    /* multiply by 16 */
  41.         dec_i += ((ind - digits) > 15) ? (ind - digits) - 6
  42.             : (ind - digits);
  43.         cp++;
  44.     }
  45.  
  46.     if ((cp - cpstart) < 8 || ind == NULL)
  47.         return 0;
  48.     if (*cp == '.')
  49.         cp++;
  50.  
  51.     cpstart = cp;
  52.     while (*cp != '\0' && (cp - cpstart) < 8 &&
  53.         (ind = index(digits, *cp)) != NULL) {
  54.         dec_f = dec_f << 4;    /* multiply by 16 */
  55.         dec_f += ((ind - digits) > 15) ? (ind - digits) - 6
  56.             : (ind - digits);
  57.         cp++;
  58.     }
  59.  
  60.     if ((cp - cpstart) < 8 || ind == NULL)
  61.         return 0;
  62.     
  63.     if (*cp != '\0' && !isspace(*cp))
  64.         return 0;
  65.  
  66.     lfp->l_ui = dec_i;
  67.     lfp->l_uf = dec_f;
  68.     return 1;
  69. }
  70.