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

  1. From: koblas@mips.COM (David Koblas)
  2. Newsgroups: alt.sources
  3. Subject: Re: conv.c -- simple numeric base converter
  4. Message-ID: <39976@mips.mips.COM>
  5. Date: 8 Jul 90 20:27:15 GMT
  6.  
  7. Yet another base converter, but written as a replacement for atof.
  8. Called as:
  9.         val = atov("0x123", 0);
  10.         val = atov("abz", 36);
  11.  
  12. Where the second argument is the default base, if == 0 then use
  13. default conventions:  0x = 16, 0 = 8, 0% = binary, else base 10.
  14.  
  15. -----CUT---HERE----------------------------------------------------------
  16.  
  17. /* +------------------------------------------------------------------+ */
  18. /* | Copyright 1989, David Koblas.                                    | */
  19. /* |   You may copy this file in whole or in part as long as you      | */
  20. /* |   don't try to make money off it, or pretend that you wrote it.  | */
  21. /* +------------------------------------------------------------------+ */
  22.  
  23. #include    <ctype.h>
  24.  
  25. #ifdef TEST
  26. main(argc,argv)
  27. int    argc;
  28. char    **argv;
  29. {
  30.     int    i;
  31.     for (i=1;i<argc;i++)
  32.         printf("%10s  == %d\n",argv[i],atov(argv[i],0));
  33. }
  34. #endif
  35.  
  36. atov(str,type)
  37. char    *str;
  38. int    type;
  39. {
  40.     int        sign = 1;
  41.     int        i;
  42.     char        c;
  43.     int        val=0,n;
  44.  
  45.     i=0;
  46.     while ((str[i]==' ') || (str[i]=='\t')) i++;
  47.     if (str[i]=='-')  {
  48.         sign = -1;
  49.         i++;
  50.     } else if (str[i]=='+') {
  51.         sign = 1;
  52.         i++;
  53.     }
  54.     if (type==0)  {
  55.         if (str[i]=='0') {
  56.             i++;
  57.             if (str[i]=='%') {
  58.                 i++;
  59.                 type=2;
  60.             } else if (str[i]=='x') {
  61.                 i++;
  62.                 type=16;
  63.             } else {
  64.                 type=8;
  65.             }
  66.         } else {
  67.             type=10;
  68.         }
  69.     }
  70.     for (;i<strlen(str);i++) {
  71.         c=str[i];
  72.         if (isdigit(c)) {
  73.             n = c - '0';
  74.         } else if (isupper(c)) {
  75.             n = c - 'A' + 10;
  76.         } else if (islower(c)) {
  77.             n = c - 'a' + 10;
  78.         } else {
  79.             goto    out;
  80.         }
  81.         if (n>=type)
  82.             goto out;
  83.         val = (val*type)+n;
  84.     }
  85. out:    
  86.     return(val * sign);
  87. }
  88.  
  89.  
  90. -- 
  91. name : David Koblas                        domain: koblas@cs.uoregon.edu
  92. place: Nowhere, I'm just an AI batch job.  domain: koblas@mips.com
  93. quote: "Time has little to do with         domain: koblas@riacs.edu
  94.     infinity and jelly donuts."        domain: koblas@stellar.arc.nasa.gov
  95.