Saturday, 6 April 2013

CONVERSION MECHANISM BETWEEN DIFFERENT NUMBER SYSTEM


In this post I will be providing a source code in C for conversions from:-
  1. Decimal to Binary number system
  2. Decimal to Hexadecimal number system
  3. Binary to Decimal number system
  4. Binary to hexadecimal number system
  5. Hexadecimal to Binary number system
  6. Hexadecimal to Decimal number system.
The following images give a brief idea about how these conversions will be carried out mathematically:-



 Decimal to Binary conversion


Decimal to Hexadecimal conversion

                                                            

Binary to Decimal conversion

Binary to Hexadecimal conversion

                                                          

Hexadecimal to Binary Conversion


Hexadecimal to Decimal conversion





Source Code:-

/***************************************************************************/
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define max 100

void d_to_b(long);
void d_to_h(void);
long b_to_d(long);
void b_to_h(void);
void h_to_d(void);
void h_to_b(void);
void printarray(void);


int a[max];

int main()
{
long num;
int ch,i;
clrscr();

for(i=0;i<max;i++)
a[i]=0;


while(1)
{
printf("YOU HAVE THE FOLLOWING CHOICES : \n\n");
printf("1.DECIMAL TO BINARY\n");
printf("2.DECIMAL TO HEXA-DECIMAL\n");
printf("3.BINARY TO DECIMAL\n");
printf("4.BINARY TO HEXA-DECIMAL\n");
printf("5.HEXA-DECIMAL TO BINARY\n");
printf("6.HEXA-DECIMAL TO DECIMAL\n");
printf("7.EXIT\n");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ENTER ANY POSETIVE DECIMAL NUMBER LESS THAN 2147483648 : ");
scanf("%ld",&num);
d_to_b(num);
printarray();
break;
case 2:
d_to_h();
break;
case 3:
printf("ENTER ANY POSETIVE BINARY NUMBER : ");
scanf("%ld",&num);
printf("%ld",b_to_d(num));
break;
case 4:
b_to_h();
break;
case 5:
h_to_b();
printarray();
break;
case 6:
h_to_d();
break;
case 7:
exit (1);
default:
printf("YOU HAVE ENTERED A WRONG CHOICE");
}
getch();
clrscr();
}



getch();
return 1;
}

void printarray(void)
{
int i,tag=0;
for(i=0;i<max;i++)
{
if(a[i]==0&&tag==0)
{

continue;
}
else
{
tag=1;
printf("%d",a[i]);
}
}
}


void d_to_b(long num)
{
int i;
for(i=max-1;i>=0;i--)
{
if(num%2==0)
a[i]=0;
else
a[i]=1;
num/=2;
}

}

void d_to_h(void)
{
int i;
long num;
printf("ENTER ANY POSETIVE DECIMAL NUMBER LESS THAN 2147483648 : ");
scanf("%lu",&num);
printf("%x\n",num);
}

long b_to_d(long num)
{
int i=0;
long result=0;
while(num)
{
result=result+(num%10)*pow(2,i++);
num/=10;
}
return result;
}


void b_to_h(void)
{
int i=0;
long num,decimal;
printf("ENTER ANY POSETIVE BINARY NUMBER LESS THAN 11111111111 : ");
scanf("%ld",&num);
decimal=b_to_d(num);
printf("%X",decimal);
}

void h_to_d(void)
{
long num;
printf("ENTER ANY POSITIVE HEXA-DECIMAL NUMBER LESS THAN OR EQUAL TO 7FFFFFFF : ");
scanf("%lx",&num);
printf("%ld",num);
}

void h_to_b()
{
long num;
printf("ENTER ANY POSITIVE HEXA-DECIMAL NUMBER LESS THAN OR EQUAL TO 7FFFFFFF : ");
scanf("%lx",&num);
d_to_b(num);
}
/*****************************************************************************/


output:-



No comments:

Post a Comment