home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / HSTR_I.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  54 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  Originally published as part of the MicroFirm Function Library
  5. **
  6. **  Copyright 1986, S.E. Margison
  7. **  Copyright 1989, Robert B.Stout
  8. **
  9. **  The user is granted a free limited license to use this source file
  10. **  to create royalty-free programs, subject to the terms of the
  11. **  license restrictions specified in the LICENSE.MFL file.
  12. **
  13. **  Make an ascii hexadecimal string into an integer.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include "numcnvrt.h"
  19.  
  20. unsigned int hstr_i(char *cptr)
  21. {
  22.       unsigned int i, j = 0;
  23.  
  24.       while (cptr && *cptr && isxdigit(*cptr))
  25.       {
  26.             i = *cptr++ - '0';
  27.             if (9 < i)
  28.                   i -= 7;
  29.             j <<= 4;
  30.             j |= (i & 0x0f);
  31.       }
  32.       return(j);
  33. }
  34.  
  35. #ifdef TEST
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42.       char *arg;
  43.       unsigned int x;
  44.  
  45.       while (--argc)
  46.       {
  47.             x = hstr_i(arg = *++argv);
  48.             printf("Hex %s = %d\n", arg, x, x);
  49.       }
  50.       return EXIT_SUCCESS;
  51. }
  52.  
  53. #endif /* TEST */
  54.