×

Hexadecimal to Binary in C

What is hexadecimal?

Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic units (including 0). (Note that here we use "16" as a decimal number to illustrate a number which would be "10" in hexadecimal) The hexadecimal numbers are 0-9, and then A-F letters are used.

What is Binary?

Binary is a system of base number 2. Base 2 ensures that only two digits 1 and 0 correspond to states 0 and 1, which is understood by the computer can. You already know the base 10-the decimal method. Decimal uses ten digits ranging from 0 to 9 and then wraps around to form two-digit numbers, with each digit 10* times larger than the last (1, 10, 100).

Examples:

Suppose the A12C number is hexadecimal. Now the binary value of the hexadecimal number is determined.

Hexadecimal number=A12D

Binary value equal to A is 1010

Binary value equal to 1 is 0001

Binary value equal to 2 is 0010

Binary value equal to D is 1101

Therefore, the binary value equivalent to A12C is 1010000100101100.

Algorithm

Start
 Step 1 -> declare function to convert Hexadecimal to Binary Number
    void convert(string hexa)
    Declare variable as long int i = 0
    Loop While(hexa[i])
       Use Switch (hexa[i])
          case '0':
             print "0000" 
             break;
          case '1':
             print "0001"
             break;
          case '2': 
             print "0010"
             break;
          case '3':
             print "0011"
             break;
          case '4':
             print "0100”
             break;
          case '5': 
             print "0101"
             break;
          case '6':
             print "0110"
             break;
          case '7':
             print "0111" 
             break;
          case '8':
             print "1000"
             break;
          case '9':
             print "1001"
             break;
          case 'A':
          case 'a': 
             print "1010"
             break;
          case 'B':
          case 'b':
             print "1011"
             break;
          case 'C':
          case 'c':
             print "1100" 
             break;
          case 'D':
          case 'd':
             print "1101"
             break;
          case 'E':
          case 'e':
             print "1110" 
             break;
          case 'F':
          case 'f':
             print "111"
             break;
          default:
             print please enter valid hexadecimal digit 
          End
       i++
    End
 Step 2 -> In main()
    Declare string hexa = "123B"
    Print convert(hexa);
 Stop 

We try to understand this with the help of an example.

#include <stdio.h> 
 // function to convert Hexadecimal to Binary Number 
 void HexToBin(char* hexdec) 
 { 
     long int i = 0; 
     while (hexdec[i]) {  
         switch (hexdec[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("1001"); 
             break; 
         case 'A': 
         case 'a':  
             printf("1010"); 
             break; 
         case 'B': 
         case 'b': 
             printf("1011"); 
             break; 
         case 'C': 
         case 'c':  
             printf("1100"); 
             break; 
         case 'D': 
         case 'd': 
             printf("1101"); 
             break; 
         case 'E': 
         case 'e':  
             printf("1110"); 
             break; 
         case 'F': 
         case 'f': 
             printf("1111"); 
             break; 
         default: 
             printf("\nInvalid hexadecimal digit %c",  
                    hexdec[i]); 
         } 
         i++; 
     } 
 } 
 // driver code  
 int main() 
 { 
     // Get the Hexadecimal number 
     char hexdec[100] = "1AC5"; 
     // Convert HexaDecimal to Binary 
     printf("\nEquivalent Binary value is : "); 
     HexToBin(hexdec); 
 }  

Output:

Hexadecimal to Binary in C

Related Topics

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.