home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / HSTR_I.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  925b  |  49 lines

  1. /*
  2. **  Originally published as part of the MicroFirm Function Library
  3. **
  4. **  Copyright 1986, S.E. Margison
  5. **  Copyright 1989, Robert B.Stout
  6. **
  7. **  Subset version released to the public domain, 1992
  8. **
  9. **  Make an ascii hexadecimal string into an integer.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <ctype.h>
  14.  
  15. unsigned int hstr_i(char *cptr)
  16. {
  17.       unsigned int i, j = 0;
  18.  
  19.       while (cptr && *cptr && isxdigit(*cptr))
  20.       {
  21.             i = *cptr++ - '0';
  22.             if (9 < i)
  23.                   i -= 7;
  24.             j <<= 4;
  25.             j |= (i & 0x0f);
  26.       }
  27.       return(j);
  28. }
  29.  
  30. #ifdef TEST
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37.       char *arg;
  38.       unsigned int x;
  39.  
  40.       while (--argc)
  41.       {
  42.             x = hstr_i(arg = *++argv);
  43.             printf("Hex %s = %d\n", arg, x, x);
  44.       }
  45.       return EXIT_SUCCESS;
  46. }
  47.  
  48. #endif /* TEST */
  49.