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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  HEXORINT.C - Detect if a string denotes a hex or decimal
  5. **  number by detecting a leading "0X" or trailing "H" string.
  6. **
  7. **  public domain demo by Bob Stout
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "sniptype.h"
  13. #include "numcnvrt.h"
  14.  
  15. /*
  16. **  Let strtol() do most of the work
  17. */
  18.  
  19. long hexorint(const char *string)
  20. {
  21.       int radix = 0;
  22.       char *dummy, valstr[128];
  23.  
  24.       strcpy(valstr, string);
  25.       if (strchr("Hh", LAST_CHAR(valstr)))
  26.       {
  27.             LAST_CHAR(valstr) = NUL;
  28.             radix = 16;
  29.       }
  30.       return strtol(valstr, &dummy, radix);
  31. }
  32.  
  33. /*
  34. **  Test code follows - compile with TEST macro defined to test
  35. */
  36.  
  37. #ifdef TEST
  38.  
  39. #include <stdio.h>
  40.  
  41. main(int argc, char *argv[])
  42. {
  43.       long val;
  44.  
  45.       while (--argc)
  46.       {
  47.             val = hexorint(*(++argv));
  48.             printf("Value of %s = %ld = %#lx\n", *argv, val, val);
  49.       }
  50.       return EXIT_SUCCESS;
  51. }
  52.  
  53. #endif /* TEST */
  54.