Wednesday, October 21, 2015

C program for converting a number to any base between 2 to 16.

     Hello friends, today I'm going to give you a program that will convert any inputted number from decimal to any specified base between 2 to 16, including the -ve numbers.

     So the first question is how to convert a number in base 10 to a specified base. The answer is rather simple. Just keep dividing the number with the new base and record the remainder each time until the quotient becomes not divisible by the base. When this happens, rearrange the remainders recorded earlier in reverse order to get the converted number. Here is an example:


Suppose we have to convert a number (234)10 to a new base say base 8.
Now proceed as follows:
 q=234/8=29        r=2
 q=29/8=3           r=5
 q=3/8=0             r=3

Now arrange the remainders calculated in reverse order, that will give: 352
This is the number in base 8 for the decimal number 234.

The following program will convert the number in base 10 to any specified base:

 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 int length(char*);          //to find the number of digits
 char* convert(int,int);     //to convert the number to given base
 char* reverse(char*);       //to reverse the digits of the number
 char* add(char*,char*,int); //to add two numbers of the given base
 char* complement(char*,int);//to find the (n-1)'s complement
 int valueof(char);          //to convert char to number
 char toChar(int);           //to convert number to character
 void main()
 {
  int n,num;
  char *con_num="";
  clrscr();
  printf("Enter the base of the new number(2-16): ");
  scanf("%d",&n);
  if(n>=2&&n<=16)
  {
   printf("Enter the number of base 10: ");
   scanf("%d",&num);
   if(num>=0){
   strcpy(con_num,convert(n,num));
   printf("The number in base %d is: %s",n,con_num);  }
   else{
   strcpy(con_num,convert(n,num*-1));
   strcpy(con_num,complement(con_num,n));
   printf("\nThe %d's complement of the number is: %s\n",n-1,con_num);
   strcpy(con_num,add(con_num,"1",n));
   printf("\nThe %d's complement of the number is: %s\n",n,con_num);
  }
 }
 else
  printf("Wrong base entered");
 getch();
}
int length(char *str)
{
 int i=0;
 while(str[i]!='\0')
  i++;
 return i;
}
char* convert(int n,int num)
{
 char *cn="";
 int i=0,r;
 if(num==0)
  cn[i++]='0';
 else
  while(num!=0)
  {
   r=num%n;
   num/=n;
   cn[i++]=toChar(r);
  }
 cn[i]='\0';
 strcpy(cn,reverse(cn));
 return cn;
}
char* reverse(char *num)
{
 int i,l,m;
 char ch;
 l=length(num);
 for(i=0,m=l-1;i<=m;i++,m--)
 {
  ch=num[i];
  num[i]=num[m];
  num[m]=ch;
 }
 return num;
}
char* complement(char *num,int n)
{
 int i=0,l;
 char ch;
 while(num[i]!='\0')
 {
  ch=num[i];
  l=(n-1)-valueof(ch);
  ch=toChar(l);
  num[i]=ch;
  i++;
 }
 return num;
}
char* add(char *num1,char *num2,int n)
{
 int i,c=0,j,l,l1=length(num1),l2=length(num2);
 char ch,*sum="";
 if(l1<l2)
 {
  num1-=(l2-l1);
  for(i=0;i<(l2-l1);i++)
   num1[i]='0';
 }
 else if(l1>l2)
 {
  num2-=(l1-l2);
  for(i=0;i<(l1-l2);i++)
   num2[i]='0';
 }
 l1=length(num1);
 l2=length(num2);
 i=l1-1;
 l2=0;
 while(i>=0)
 {
  l=valueof(num1[i]);
  j=valueof(num2[i]);
  j=(l+j+c);
  if(j>=n)
  {
   j=j-n;
   c=1;
  }
  else
  {
   if(c==1)
    c=0;
  }
  ch=toChar(j);
  sum[l2++]=ch;
  i--;
 }
 if(c==1)
 {
  sum[l2++]='1';
  c=0;
 }
 sum[l2]='\0';
 strcpy(sum,reverse(sum));
 return sum;
}
int valueof(char ch)
{
 int val;
 switch(ch)
 {
  case '0':
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9': val=ch-'0';break;
  case 'A':
  case 'B':
  case 'C':
  case 'D':
  case 'E':
  case 'F': val=ch-55;break;
 }
 return val;
}
char toChar(int num)
{
 char ch;
 switch(num)
 {
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
  case 7:
  case 8:
  case 9: ch='0'+num;break;
  case 10:
  case 11:
  case 12:
  case 13:
  case 14:
  case 15:ch=55+num;break;
 }
 return ch;
}


Hope you will find it useful. I'll post a new program soon. Just keep visiting here. See yall soon....

No comments:

Post a Comment