home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1547 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  2.3 KB

  1. From: jef@well.sf.ca.us (Jef Poskanzer)
  2. Newsgroups: alt.sources
  3. Subject: Re: conv.c -- simple numeric base converter
  4. Message-ID: <18852@well.sf.ca.us>
  5. Date: 3 Jul 90 15:05:12 GMT
  6.  
  7. For such a simple interface, a script front-ending for bc would
  8. work too.  For a somewhat more interesting interface, see below.
  9. It's a base-conversion filter -- looks for any numbers in stdin,
  10. converts them from any base to any base in [2..36], and leaves
  11. non-numeric text alone.
  12. ---
  13. Jef
  14.  
  15.   Jef Poskanzer  jef@well.sf.ca.us  {ucbvax, apple, hplabs}!well!jef
  16.        "Publish and be damned." -- Wellesley, Duke of Wellington
  17.  
  18. /*
  19. ** baseconvert.c - base conversion filter
  20. **
  21. ** Copyright (C) 1989 by Jef Poskanzer.
  22. **
  23. ** Permission to use, copy, modify, and distribute this software and its
  24. ** documentation for any purpose and without fee is hereby granted, provided
  25. ** that the above copyright notice appear in all copies and that both that
  26. ** copyright notice and this permission notice appear in supporting
  27. ** documentation.  This software is provided "as is" without express or
  28. ** implied warranty.
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <strings.h>
  33. #include <ctype.h>
  34.  
  35. static char *idigits = "0123456789abcdefghijklmnopqrstuvwxyz";
  36. static char *odigits = "0123456789abcdefghijklmnopqrstuvwxyz";
  37.  
  38. main( argc, argv )
  39. int argc;
  40. char *argv[];
  41.     {
  42.     int ibase, obase;
  43.     char *usage = "usage: %s <ibase> <obase>\n";
  44.     int ic, i, gettingi, digit;
  45.     char c, lc;
  46.     char *digitp;
  47.     void putint( );
  48.  
  49.     if ( argc != 3 ||
  50.      ( ibase = atoi( argv[1] ) ) < 2 || ibase > 36 ||
  51.      ( obase = atoi( argv[2] ) ) < 2 || obase > 36 )
  52.     {
  53.     fprintf( stderr, usage, argv[0] );
  54.     exit( 1 );
  55.     }
  56.     idigits[ibase] = '\0';
  57.  
  58.     gettingi = 0;
  59.     for ( ; ; )
  60.     {
  61.     ic = getchar( );
  62.     if ( ic == EOF ) break;
  63.     c = ic;
  64.     if ( isupper( c ) )
  65.         lc = tolower( c );
  66.     else
  67.         lc = c;
  68.     digitp = index( idigits, lc );
  69.     if ( digitp == (char *) 0 )
  70.         {
  71.         if ( gettingi )
  72.         {
  73.         putint( i, obase );
  74.         gettingi = 0;
  75.         }
  76.         putchar( c );
  77.         }
  78.     else
  79.         {
  80.         digit = digitp - idigits;
  81.         if ( gettingi )
  82.         {
  83.         i = i * ibase + digit;
  84.         }
  85.         else
  86.         {
  87.         gettingi = 1;
  88.         i = digit;
  89.         }
  90.         }
  91.     }
  92.     
  93.     if ( gettingi )
  94.     putint( i, obase );
  95.  
  96.     exit( 0 );
  97.     }
  98.  
  99. void
  100. putint( i, base )
  101. int i, base;
  102.     {
  103.     if ( i >= base )
  104.     putint( i / base, base );
  105.     putchar( odigits[i % base] );
  106.     }
  107.