How to do it…

  1. Enter a binary number and assign it to the b variable:
printf("Enter a number in binary number ");
scanf("%d",&b);
  1. Invoke the intodecimal function to convert the binary number into a decimal number, and pass the b variable to it as an argument. Assign the decimal number returned by the intodecimal function to the d variable:
d=intodecimal(b);
  1. On looking at the intodecimal definition, int intodecimal(int bin) { }, we can see that the b argument is assigned to the bin parameter of the intodecimal function.
  2. Separate all the binary digits and multiply them by 2 raised to the power of their position in the binary number. Sum the results to get the decimal equivalent. To separate each binary digit, we need to execute a while loop until the binary number is greater than 0:
while(bin >0)
  1. Within the while loop, apply the mod 10 operator on the binary number and push the remainder to the stack:
remainder=bin%10;
push(remainder);
  1. Execute another while loop to get the decimal number of all the binary digits from the stack. The while loop will execute until the stack becomes empty (that is, until the value of top is greater than or equal to 0):
while(top >=0)
  1. In the while loop, pop off all the binary digits from the stack and multiply each one by 2 raised to the power of top. Sum the results to get the decimal equivalent of the entered binary number:
j=pop();
deci=deci+j*pow(2,exp);
  1. Invoke the intohexa function and pass the binary number and the decimal number to it to get the hexadecimal number:
void intohexa(int bin, int deci)
  1. Apply the mod 16 operator in the intohexa function on the decimal number to get its hexadecimal. Push the remainder that you get to the stack. Apply mod 16 to the quotient again and repeat the process until the quotient becomes smaller than 16:
remainder=deci%16;
push(remainder);
  1. Pop off the remainders that are pushed to the stack to display the hexadecimal number:
j=pop();

If the remainder that is popped off from the stack is less than 10, it is displayed as such. Otherwise, it is converted to its equivalent letter, as mentioned in the following table, and the resulting letter is displayed:

Decimal Hexadecimal
10 A
11 B
12 C
13 D
14 E
15 F

 

if(j<10)printf("%d",j);
else printf("%c",prnhexa(j));

The binarytohexa.c program explains how a binary number can be converted into a hexadecimal number:

//Converting binary to hex
# include <stdio.h>
#include <math.h>
#define max 10
int top=-1;
int stack[max];
void push();
int pop();
char prnhexa(int);
int intodecimal(int);
void intohexa(int, int);
void main()
{
int b,d;
printf("Enter a number in binary number ");
scanf("%d",&b);
d=intodecimal(b);
printf("The decimal of binary number %d is %d ", b, d);
intohexa(b,d);
}
int intodecimal(int bin)
{
int deci, remainder,exp,j;
while(bin >0)
{
remainder=bin%10;
push(remainder);
bin=bin/10;
}
deci=0;
exp=top;
while(top >=0)
{
j=pop();
deci=deci+j*pow(2,exp);
exp--;
}
return (deci);
}
void intohexa(int bin, int deci)
{
int remainder,j;
while(deci >0)
{
remainder=deci%16;
push(remainder);
deci=deci/16;
}
printf("The hexa decimal format of binary number %d is ",bin);
while(top >=0)
{
j=pop();
if(j<10)printf("%d",j);
else printf("%c",prnhexa(j));
}
}
void push(int m)
{
top++;
stack[top]=m;
}
int pop()
{
int j;
if(top==-1)return(top);
j=stack[top];
top--;
return(j);
}
char prnhexa(int v)
{
switch(v)
{
case 10: return ('A');
break;
case 11: return ('B');
break;
case 12: return ('C');
break;
case 13: return ('D');
break;
case 14: return ('E');
break;
case 15: return ('F');
break;
}
}

Now, let's go behind the scenes. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset