home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / DDMC100.ZIP / DDMC.C next >
C/C++ Source or Header  |  1993-09-08  |  5KB  |  198 lines

  1. /* ddmc.c */
  2. /****************************************************************************
  3. Doug Deep's MegaNum Converter ■ version 1
  4.  
  5. Freeware from Duane Paulson. A "MegaNum" is what the RIPscrip people call a
  6. base 36 number. These are used in RIPscrips to allow data to be transmitted
  7. in a compact format. Unfortunately, base-36 conversions are far from
  8. intuitive, and in fact can lead to nightmare situations, as in Flag 1 of
  9. the Button Descriptor, which is a 4 digit MegaNum, bitmapped! In order to
  10. ease the pain of dealing with MegaNums, this program is presented. It
  11. accepts input in Binary, Decimal, Hexadecimal, or MegaNum format, then
  12. translates and displays the values for each of these for base-number systems.
  13.  
  14. [RIPscrip is a trademark of TeleGrafix Communications, Inc., which had nothing
  15. to do with the making of this program.]
  16.  
  17. Contacting the Author (Duane Paulson):
  18. o Leave a message to DUANE PAULSON on the Main Board of The Kandy Shack BBS
  19.   (714)636-2662 -- Garden Grove, CA.
  20. o Or, you might try leaving a message to DUANE PAULSON in the ILink Shareware
  21.   Conference.
  22. o ci922@cleveland.freenet.edu (Duane A. Paulson)
  23. ****************************************************************************/
  24.  
  25. /****************************************************************************
  26. HISTORY:
  27.  
  28. 1.00 09/08/93:
  29. Initial release of ddmc.
  30. ****************************************************************************/
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #define BUFSIZE 1023
  36.  
  37.  
  38. char buf[BUFSIZE+1];
  39. char bindigs[]="01";
  40. char decdigs[]="0123456789";
  41. char hexdigs[]="0123456789ABCDEF";
  42. char meganums[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  43. char binnums[16][5]={
  44.     "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
  45.     "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };
  46. char hexs[7];
  47. char binprompt[]="Binary";
  48. char maxbinval[]="1 1001 1010 0000 1111 1111";
  49. int  maxbindigits=21;
  50. char decprompt[]="Decimal";
  51. char maxdecval[]="1,679,615";
  52. int  maxdecdigits=7;
  53. char hexprompt[]="Hexadecimal";
  54. char maxhexval[]="19 A0 FF";
  55. int  maxhexdigits=6;
  56. char megprompt[]="MegaNum";
  57. char maxmegval[]="ZZZZ";
  58. int  maxmegdigits=4;
  59.  
  60. main()
  61. {
  62.     long  d, s;
  63.     int   e, maxdigs, base, nogood;
  64.     char *b, *digits, *maxval, *prompt, *q;
  65.  
  66.     printf("Doug Deep's MegaNum Converter ■ version 1.00 ■ 09/08/93\n\n");
  67.  
  68.     printf("Freeware from Duane Paulson. A \"MegaNum\" is what the RIPscrip people call a\n");
  69.     printf("base 36 number. These are used in RIPscrips to allow data to be transmitted\n");
  70.     printf("in a compact format. Unfortunately, base-36 conversions are far from\n");
  71.     printf("intuitive, and in fact can lead to nightmare situations, as in Flag 1 of\n");
  72.     printf("the Button Descriptor, which is a 4 digit MegaNum, bitmapped! In order to\n");
  73.     printf("ease the pain of dealing with MegaNums, this program is presented. It\n");
  74.     printf("accepts input in Binary, Decimal, Hexadecimal, or MegaNum format, then\n");
  75.     printf("translates and displays the values for each of these for base-number systems.\n\n");
  76.  
  77.     printf("[RIPscrip is a trademark of TeleGrafix Communications, Inc., which had nothing\n");
  78.     printf("to do with the making of this program.]\n");
  79.  
  80.     while(1)
  81.     {
  82.         *buf='z';
  83.         while(strchr("bdhm\n", *buf)==NULL)
  84.         {
  85.             printf("\nSelect format for input\n");
  86.             printf("\tB)inary. . . . .base 2\n");
  87.             printf("\tD)ecimal . . . .base 10\n");
  88.             printf("\tH)exadecimal . .base 16\n");
  89.             printf("\tM)egaNum . . . .base 36\n");
  90.             printf("Enter Selection (CR alone exits):");
  91.  
  92.             fgets(buf, 3, stdin);
  93.             *buf=(char)tolower(*buf);
  94.         }
  95.  
  96.         if(*buf=='\n')
  97.             exit(0);
  98.  
  99.         if(*buf=='b')
  100.         {
  101.             base=2;
  102.             prompt=binprompt;
  103.             digits=bindigs;
  104.             maxval=maxbinval;
  105.             maxdigs=maxbindigits;
  106.         }
  107.  
  108.         if(*buf=='d')
  109.         {
  110.             base=10;
  111.             prompt=decprompt;
  112.             digits=decdigs;
  113.             maxval=maxdecval;
  114.             maxdigs=maxdecdigits;
  115.         }
  116.  
  117.         if(*buf=='h')
  118.         {
  119.             base=16;
  120.             prompt=hexprompt;
  121.             digits=hexdigs;
  122.             maxval=maxhexval;
  123.             maxdigs=maxhexdigits;
  124.         }
  125.  
  126.         if(*buf=='m')
  127.         {
  128.             base=36;
  129.             prompt=megprompt;
  130.             digits=meganums;
  131.             maxval=maxmegval;
  132.             maxdigs=maxmegdigits;
  133.         }
  134.  
  135.         while(1)
  136.         {
  137.             printf("\n%s value to convert (CR alone exits): ", prompt);
  138.             fgets(buf, BUFSIZE, stdin);
  139.  
  140.             if(*buf=='\n')
  141.                 break;
  142.  
  143.             if((s=strlen(buf))>(maxdigs+1))
  144.             {
  145.                 fprintf(stderr, "Number out of range (0 - %s)\n", maxval);
  146.                 continue;
  147.             }
  148.  
  149.             strupr(buf);
  150.             d=0;
  151.  
  152.             nogood=0;
  153.             for(b=buf; *b!='\n'; b++)
  154.             {
  155.                 if((q=strchr(digits, *b))==NULL)
  156.                 {
  157.                     printf("Illegal digit `%c' (ascii %d)\n", *b, *b);
  158.                     nogood++;
  159.                 }
  160.  
  161.                 d*=base;
  162.                 d+=(long)(q-digits);
  163.             }
  164.             if(nogood)
  165.                 continue;
  166.  
  167.             if(d>1679615L)
  168.             {
  169.                 fprintf(stderr, "Number out of range (0 - %s)\n", maxval);
  170.                 continue;
  171.             }
  172.             printf("dec: %ld\n", d);
  173.             sprintf(hexs, "%lX\n", d);
  174.             printf("hex: %s", hexs);
  175.  
  176.             printf("bin: ");
  177.             for(b=hexs; *b!='\n'; b++)
  178.                 printf("%s ", binnums[strchr(hexdigs, *b)-hexdigs]);
  179.             printf("\n");
  180.  
  181.             printf("meg: ");
  182.             e=(int)(d/46656L);
  183.             d%=46656L;
  184.             printf("%c", meganums[e]);
  185.  
  186.             e=(int)(d/1296L);
  187.             d%=1296L;
  188.             printf("%c", meganums[e]);
  189.  
  190.             e=(int)(d/36L);
  191.             d%=36L;
  192.             printf("%c%c\n", meganums[e], meganums[d]);
  193.  
  194.         }
  195.     }
  196. }
  197. /* end ddmc.c */
  198.