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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  RDXCNVRT.C - Convert between number bases
  5. **
  6. **  public domain demo by Bob Stout
  7. **  uses LTOA.C, also in SNIPPETS
  8. */
  9.  
  10. #include <stdlib.h>
  11. #ifdef TEST
  12.  #include <stdio.h>
  13. #endif
  14.  
  15. char *ltoa(long num, char *buf, int base);
  16.  
  17. /*
  18. **  Calling parameters: 1 - Number string to be converted
  19. **                      2 - Buffer for the  converted output
  20. **                      3 - Radix (base) of the input
  21. **                      4 - Radix of the output
  22. **
  23. **  Returns: Pointer to converted output
  24. */
  25.  
  26. char *radix_convert(const char *in, char *out, int rin, int rout)
  27. {
  28.       long n;
  29.       char *dummy;
  30.  
  31.       n = strtol(in, &dummy, rin);
  32.       return ltoa(n, out, rout);
  33. }
  34.  
  35. #ifdef TEST
  36.  
  37. int main(int argc, char *argv[])
  38. {
  39.       int rin, rout;
  40.       char buf[40];
  41.  
  42.       if (4 > argc)
  43.       {
  44.             puts("Usage: RDXCNVRT <number> <base_in> <base_out>");
  45.             return(-1);
  46.       }
  47.       rin  = atoi(argv[2]);
  48.       rout = atoi(argv[3]);
  49.       printf("%s (base %d) = %s (base %d)\n", argv[1], rin,
  50.             radix_convert((const char *)argv[1], buf, rin, rout), rout);
  51.       return 0;
  52. }
  53.  
  54. #endif /* TEST */
  55.