Write a C Program convert from any base to any other base.

/*
 * Convert from any base to any other base.
 */
#include 
#include 
#include 
#include 

#define  MINBASE   2u               /* smallest base */
#define  MAXBASE  36u               /* largest base */

int main()
{
  void displayValueAndBases(unsigned long num, int base, int newbase);
  long getNumberInBaseN(unsigned int b);
  int  getBase(unsigned int minbase, unsigned int maxbase);

  int  base;                        /* initial base */
  int  newbase;                     /* final base*/
  unsigned long num;                /* initial value, in base 10 */

  printf("Enter initial base: ");   /* grab initial base (in base 10) */
  if ((base = getBase(MINBASE, MAXBASE)) != -1)
  {                                
    printf("Enter target base: ");             /* grab target base */
    if ((newbase = getBase(MINBASE, MAXBASE)) != -1)
    {
      printf("Enter number to convert: ");     /* grab number */
      while ((num = getNumberInBaseN(base)) != -1)
      {                             
        displayValueAndBases(num, base, newbase);
        printf("Enter another base %i number to convert: ", base);
      }
      putchar('\n');
    }
  }

  return EXIT_SUCCESS;
}


void displayValueAndBases(unsigned long num, int base, int newbase)
{
  void displayValueInBase(unsigned long v, unsigned int b);

  displayValueInBase(num, base);
  printf(" in base %i is ", base);
  displayValueInBase(num, newbase);
  printf(" in base %i.\n", newbase);
}


/*
 * Read in the base using getNumberInBaseN
 */

int getBase(unsigned int minbase, unsigned int maxbase)
{
  long getNumberInBaseN(unsigned int b);

  long base;

  if ((base = getNumberInBaseN(10)) == -1)
    printf("Error: nonnumeric base detected\n");
  else
  {
    if (base < minbase)             /* set to -1 if too small */
    {
      printf("Error: Smallest base is %u.\n", minbase);
      base = -1;
    }
    if (base > maxbase)             /* set to -1 if too large */
    {
      printf("Error: Largest base is %u.\n", maxbase);
      base = -1;
    }
  }
  return (int) base;                /* base is in range of int */
}



/*
 * Read a base N number, one character at a time.
 */
              
long getNumberInBaseN(unsigned int base)
{
  int  toDecimal(int c, int b);

  int  c;                          /* next input character */
  int  value;                      /* decimal value of next char */
  long sum;                        /* running total */

  sum = -1;
  if ((c = getchar()) != EOF)      /* EOF is error */
  {                       
    if ((value = toDecimal(c, base)) != -1)           
    {                              /* within number, compute sum */
      sum = 0;
      while (value != -1)        
      {                            /* valid next digit in base */
        sum = base * sum + value;  /* update number */
        c = getchar();
        value = toDecimal(c, base);
      }
    }
    if (c != '\n')             
    {
      sum = -1;                    /* indicate error! */
      while (c != '\n')            /* skip rest of line */
        c = getchar();   
    }
  }
  return sum;
}


/*
 * Functions to convert digits to and from characters (ASCII ONLY).
 *   toDecimal - convert char to equivalent decimal number.
 *   toChar - convert decimal number to equivalent character.
 */

int toDecimal(int c, int base) 
{
  int value;

  if (isdigit(c))               /* set value if the input is anything */
    value = c - '0';            /* we know how to handle */
  else
    if (islower(c))
      value = c - 'a' + 10;
    else 
      if (isupper(c))
        value = c - 'A' + 10;
      else                      /* if not, it's an error */
        value = -1;

  if (value >= base)            /* make sure it's legal for base */
    value = -1;

  return value;
}

int toChar(int value)          
{                       
  int c;

  if (value < 10)
    c = '0' + value;
  else
    c = 'A' + value - 10;
  return c;
}



/*
 * Display a value in a specified base between 2 and 36.
 */

void displayValueInBase(unsigned long v, unsigned int b)
{
  int toChar(int value);           /* Addition to Chapter 4 */

  unsigned int k;                  /* digits needed in result */
  unsigned long divisor;           /* initially b^(# of digits - 1) */

  if (v == 0)                      /* zero is the same in any base */
    printf("0");
  else
  {
    k = floor(log10(v)/log10(b)) + 1;
    divisor = pow(b, k - 1);       /* first divisor: b^(k - 1) */

    /* Run through value, calculating and displaying the value of each
       of the digits in the new base (left to right) */

    while (divisor >= 1)      
    {
      putchar(toChar((int)(v / divisor)));  /* Change from Chapter 4 */
      v = v % divisor;
      divisor = divisor / b;
    }
  }
}

1 comment:

  1. what if we want to convert a number from base X to base Y.
    Base X, Y : range 2 .. 20.
    positive whole number (i.e. 1451, no 1.43x10^6)

    ReplyDelete