×

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

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.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

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

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.