×

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

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 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.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.