Binary to Hexadecimal Conversion

The number system conversion C program which converts binary number to a hexadecimal number, is called binary to hexadecimal conversion. alternatively, it is said be a conversion from base-2 to base-16 number system or radix-2 to radix-16 number system conversion.


binary to hexadecimal number

Binary to Hexadecimal - algorithm

          set hexa =[]            

          Read  binary_bits form user                  
          reverse(binary_bits)
     
          while not EOF(binary_bits)
             
             read 4-bits from binary_bits
             convert 4-bits to decimal
             append decimal to hexa             

          end    
         
         print reverse(hexa)      
   

Binary to Hexadecimal conversion example

    
       The given binary number is 11101101010110 and 
           converts it into hexadecimal number

         binary = 11 1011 0101 0110,  hexadecimal = ?, 
            
         
         1.  split binary numbers into 4 bits group from right to left direction              
         2.  carried out binary to decimal conversion on each 4 bits group  
         3.  convert each decimal digit into equivalent hexadecimal digit      
 
        if left most group is less then 4 bits, 0's are added  
                

       binary      =   0011 | 1011 | 0101 | 0110
       decimal     =     3  |   11 |   5  |   6  
       hexadecimal =     3       B      5     6

      if binary = 01101010010, equivalent hexadecimal = 0x3B56
 
  

Binary to Hexadecimal - C program code

The C program converts binary to hexadecimal number. The program reads binary numbers from user as input, carry out conversion process and returns a hexadecimal number as result.
 

#include<stdio.h>
#include<string.h>
#include<math.h>

int bin2dec(char *bits,int start,int len) {
    
    int decval=0; char bit;
    int end = start + len-1;   
    for(int n=0;n<len;n++) 
      {  
       bit = bits[end-n] =='1' ? 1 : 0;   
       if ( bit==1)
       decval+= pow(2,n);             
      }  
   return decval;  
}


int main ()
{

   char hexc[] = {'0','1','2','3','4','5',
                 '6','7','8','9','A','B',
                 'C','D','E','F'};
      
   char bits[17]; int len =0;
   char hex[5]; int k=0;
   
   printf("\n Binary to Hexadecimal Conversion");
   
   printf("\n Enter Binary bits :") ;
   scanf("%s",&bits);

   len = strlen(bits);
   int startb=len;  int hbs=4;
   
    while (startb>0) {
 
   startb -=hbs; 
   if ( startb < 0  ) {
       startb +=hbs;
          hbs = startb - 0;    
    startb -=hbs; 
   }       
   
   int decval = bin2dec(bits,startb,hbs);   
   //printf ("\n%d \t %d \t %d %c",startb,hbs,decval,hexc[decval]);
   
   hex[k++]= hexc[decval];       
 }
    hex[k]='\0';
   
   printf("\nHexadecimal value :%s",strrev(hex));   
   
   return 0;
}

 

Binary to Hexadecimal - C program code Output


Binary to Hexadecimal Conversion
Enter Binary bits :010110101110

Hexadecimal value :5AE





Download c program source code

Comments

Popular posts from this blog

Convert Octal to Binary

Hexadecimal to Binary Conversion

C program Simple Interest and Compound Interest