C Program to Convert Binary to Hexadecimal 1


#include<stdio.h>

int main(){

 

long int binaryNumber,hexadecimalNumber=0,j=1,remainder;

 

printf(“Enter any number any binary number: “);

scanf(“%ld”,&binaryNumber);

 

while(binaryNumber!=0){

remainder=binaryNumber%10;

hexadecimalNumber=hexadecimalNumber+remainder*j;

j=j*2;

binaryNumber=binaryNumber/10;

}

 

printf(“Equivalent hexadecimal value: %lX”,hexadecimalNumber);

return 0;

}

Sample output:

Enter any number any binary number: 1101

Equivalent hexadecimal value: D

How to convert large binary number to hexadecimal

#include<stdio.h>

#define MAX 1000

int main(){

char binaryNumber[MAX],hexaDecimal[MAX];

int temp;

long int i=0,j=0;

 

printf(“Enter any number any binary number: “);

scanf(“%s”,binaryNumber);

while(binaryNumber[i]){

binaryNumber[i] = binaryNumber[i] -48;

++i;

}

–i;

while(i-2>=0){

temp =  binaryNumber[i-3] *8 + binaryNumber[i-2] *4 +  binaryNumber[i-1] *2 + binaryNumber[i] ;

if(temp > 9)

hexaDecimal[j++] = temp + 55;

else

hexaDecimal[j++] = temp + 48;

i=i-4;

}

if(i ==1)

hexaDecimal[j] = binaryNumber[i-1] *2 + binaryNumber[i] + 48 ;

else if(i==0)

hexaDecimal[j] =  binaryNumber[i] + 48 ;

else

–j;

printf(“Equivalent hexadecimal value: “);

while(j>=0){

printf(“%c”,hexaDecimal[j–]);

}

return 0;

}

Sample output:

Enter any number any binary number: 1010011011100011110

001001111011110001000100011101110111011110

Equivalent hexadecimal value: 14DC789EF111DDDE
Algorithm:

Binary to hexadecimal conversion method

Step1: Arrange the binary number in the group 4 from right side.

Step 2:  Replace the each group with following values:

Binary number Hexadecimal values
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

Binary to hexadecimal chart

Binary to hexadecimal conversion examples:

For example we want to convert binary number 11011010101001101 to hexadecimal.

Step 1: 0001 1011 0101 0100 1101

Step 2:  1     B    5    4   D

So (1011010101001101)2 = (1B54D)16


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “C Program to Convert Binary to Hexadecimal