Search

Hexadecimal to Binary in C || C Programs

What is hexadecimal?

The hexadecimal is a positional system that represents the numbers in sixteen distinct symbols by using 16 as a base. These distinct symbols, i.e., "0-9" represents the values from zero to nine, and "A-F" represents the values from ten to fifteen.


What is Binary?

The binary is a number which is represented with a base 2 numeral system, and it uses two symbols, i.e., 0 and 1.


Code -

#include<stdio.h> 
void hextobin(char b[]);  
void main()  
{  
 char hex[]="A12C";  
    hextobin(hex);  
}  
void hextobin(char hex[])  
{  
 int i=0;   
 while(hex[i])  
   {  
 switch(hex[i])  
       {  
 case '0':  
           printf("0000");  
 break;  
 case '1':  
           printf("0001");  
 break;  
 case '2':  
           printf("0010");  
 break;  
 case '3':  
           printf("0011");  
 break;  
 case '4':  
           printf("0100");  
 break;  
 case '5':  
           printf("0101");  
 break;  
 case '6':  
           printf("0110");  
 break;  
 case '7':  
           printf("0111");  
 break;  
 case '8':  
           printf("1000");  
 break;  
 case '9':  
           printf("1000");  
 break;  
 case 'A':  
           printf("1010");  
 break;  
 case 'a':  
           printf("1010");  
 break;  
 case 'B':  
           printf("1011");  
 break;  
 case 'b':  
           printf("1011");  
 break;  
 case 'C':  
           printf("1100");  
 break;  
 case 'c':  
           printf("1100");  
 break;  
 case 'D':  
           printf("1101");  
 break;  
 case 'd':  
           printf("1101");  
 break;  
 case 'E':  
           printf("1110");  
 break;  
 case 'e':  
           printf("1110");  
 break;  
 case 'F':  
           printf("1111");  
 break;  
 case 'f':  
           printf("1111");  
 break;  
       }  
       i++;  
   }}  

Output -

1010000100101100
6 views0 comments

Related Posts

See All

Program of Random Number in C || C Programs

Code - #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { // use rand() function to generate the number printf (" The random number is: %d", rand()); pri

Getchar() function in C || C Programs

Code - #include <stdio.h> #include <conio.h> void main() { char c; printf ("\n Enter a character \n"); c = getchar(); // get a single character printf(" You have passed ")

  • 1024px-Telegram_logo.svg
  • download
  • YouTube