asm/decimal.c
changeset 5 d752cbe8208e
child 9 c3fecc82ade6
equal deleted inserted replaced
4:236f8f747073 5:d752cbe8208e
       
     1 /**
       
     2  * $Id: decimal.c,v 1.1.1.1 2008-04-28 17:32:52 mbroeker Exp $
       
     3  * $Source: /development/c/asm/decimal.c,v $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <stdlib.h>
       
     9 #include <string.h>
       
    10 #include <limits.h>
       
    11 
       
    12 #define  MAXBITS __WORDSIZE
       
    13 #define  MAXVALUE ULONG_MAX
       
    14 
       
    15 typedef unsigned long int ULONG;
       
    16 
       
    17 void usage (char *name)
       
    18 {
       
    19     printf ("Usage: %s start [end] [base]\n", name);
       
    20 }
       
    21 
       
    22 /*
       
    23  * Computes the binary representation of a
       
    24  * decimal value and returns the result
       
    25  * in proper order and the number of digits
       
    26  */
       
    27 
       
    28 char *dec_to_base (ULONG decimal, int base, int *digits)
       
    29 {
       
    30     char *encoded;
       
    31     char swp;
       
    32     int i;
       
    33 
       
    34     i = 0;
       
    35 
       
    36     if ((decimal > MAXVALUE)) {
       
    37         perror ("VALUE TOO BIG");
       
    38         return NULL;
       
    39     }
       
    40 
       
    41     if ((encoded = malloc ((MAXBITS) * sizeof (char))) == NULL) {
       
    42         perror ("MALLOC ERROR");
       
    43         return NULL;
       
    44     }
       
    45 
       
    46     while (decimal > base - 1) {
       
    47         encoded[i++] = (decimal % base);
       
    48         decimal /= base;
       
    49     }
       
    50     encoded[i++] = (decimal % base);
       
    51     *digits = i;
       
    52 
       
    53     for (i = 0; i < *digits - 1; i++) {
       
    54         swp = encoded[*digits - i - 1];
       
    55         encoded[*digits - i - 1] = encoded[i];
       
    56         encoded[i] = swp;
       
    57     }
       
    58 
       
    59     return encoded;
       
    60 }
       
    61 
       
    62 int main (int argc, char **argv)
       
    63 {
       
    64     ULONG decimal, start, end;
       
    65     int bits, base, i;
       
    66     char *binaer;
       
    67 
       
    68     base = 2;
       
    69 
       
    70     if (argc == 2) {
       
    71         start = atol (argv[1]);
       
    72         end = start + 1;
       
    73     } else if (argc == 3) {
       
    74         start = atol (argv[1]);
       
    75         end = atol (argv[2]);
       
    76     } else if (argc == 4) {
       
    77         start = atol (argv[1]);
       
    78         end = atol (argv[2]);
       
    79         base = atoi (argv[3]);
       
    80     } else {
       
    81         usage (argv[0]);
       
    82         return EXIT_SUCCESS;
       
    83     }
       
    84 
       
    85     if (!((start >= 0) && (end > start) && (base > 1))) {
       
    86         usage ("Trottel");
       
    87         printf ("%ld\t%ld\t%d\n", start, end, base);
       
    88         return EXIT_SUCCESS;
       
    89     }
       
    90 
       
    91     printf ("\tVALUE \t\tDIGITS\t\t Encoded\n");
       
    92     printf ("----------------------------------------------------\n");
       
    93 
       
    94     for (decimal = start; decimal < end; decimal++) {
       
    95         if ((binaer = dec_to_base (decimal, base, &bits)) == NULL) {
       
    96             return EXIT_FAILURE;
       
    97         }
       
    98 
       
    99         printf ("%12ld %12d \t\t", decimal, bits);
       
   100 
       
   101         for (i = 0; i < bits; i++) {
       
   102             printf ("%X", binaer[i]);
       
   103         }
       
   104         printf ("\n");
       
   105 
       
   106         if (binaer)
       
   107             free (binaer);
       
   108     }
       
   109     return EXIT_SUCCESS;
       
   110 }