# Program to Convert Hexadecimal Number to Binary

Given a Hexadecimal number as an input, the task is to convert that number to Binary number.

Examples:

```Input: Hexadecimal = 1AC5
Output: Binary = 0001101011000101
Explanation:
Equivalent binary value of 1: 0001
Equivalent binary value of A: 1010
Equivalent binary value of C: 1100
Equivalent binary value of 5: 0101

Output: Binary = 0101110100011111
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: A hexadecimal number is a positional numeral system with a radix, or base, of 16 and uses sixteen distinct symbols.

A binary number is a number expressed in the base-2 binary numeral system, which uses only two symbols: which are 0 (zero) and 1 (one).

To convert HexaDecimal number to Binary, the binary equivalent of each digit of the HexaDecimal number is evaluated and combined at the end to get the equivalent binary number.

Below is the implementation of the above approach:

## C++

 `// C++ program to convert ` `// Hexadecimal number to Binary ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// function to convert ` `// Hexadecimal to Binary Number ` `void` `HexToBin(string hexdec) ` `{ ` `    ``long` `int` `i = 0; ` ` `  `    ``while` `(hexdec[i]) { ` ` `  `        ``switch` `(hexdec[i]) { ` `        ``case` `'0'``: ` `            ``cout << ``"0000"``; ` `            ``break``; ` `        ``case` `'1'``: ` `            ``cout << ``"0001"``; ` `            ``break``; ` `        ``case` `'2'``: ` `            ``cout << ``"0010"``; ` `            ``break``; ` `        ``case` `'3'``: ` `            ``cout << ``"0011"``; ` `            ``break``; ` `        ``case` `'4'``: ` `            ``cout << ``"0100"``; ` `            ``break``; ` `        ``case` `'5'``: ` `            ``cout << ``"0101"``; ` `            ``break``; ` `        ``case` `'6'``: ` `            ``cout << ``"0110"``; ` `            ``break``; ` `        ``case` `'7'``: ` `            ``cout << ``"0111"``; ` `            ``break``; ` `        ``case` `'8'``: ` `            ``cout << ``"1000"``; ` `            ``break``; ` `        ``case` `'9'``: ` `            ``cout << ``"1001"``; ` `            ``break``; ` `        ``case` `'A'``: ` `        ``case` `'a'``: ` `            ``cout << ``"1010"``; ` `            ``break``; ` `        ``case` `'B'``: ` `        ``case` `'b'``: ` `            ``cout << ``"1011"``; ` `            ``break``; ` `        ``case` `'C'``: ` `        ``case` `'c'``: ` `            ``cout << ``"1100"``; ` `            ``break``; ` `        ``case` `'D'``: ` `        ``case` `'d'``: ` `            ``cout << ``"1101"``; ` `            ``break``; ` `        ``case` `'E'``: ` `        ``case` `'e'``: ` `            ``cout << ``"1110"``; ` `            ``break``; ` `        ``case` `'F'``: ` `        ``case` `'f'``: ` `            ``cout << ``"1111"``; ` `            ``break``; ` `        ``default``: ` `            ``cout << ``"\nInvalid hexadecimal digit "` `                 ``<< hexdec[i]; ` `        ``} ` `        ``i++; ` `    ``} ` `} ` ` `  `// driver code ` `int` `main() ` `{ ` ` `  `    ``// Get the Hexadecimal number ` `    ``char` `hexdec[100] = ``"1AC5"``; ` ` `  `    ``// Convert HexaDecimal to Binary ` `    ``cout << ``"\nEquivalent Binary value is : "``; ` `    ``HexToBin(hexdec); ` ` `  `    ``return` `0; ` `} `

## C

 `// C program to convert ` `// Hexadecimal number to Binary ` ` `  `#include ` ` `  `// 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); ` `} `

## Java

 `// Java program to convert ` `// Hexadecimal number to Binary ` ` `  `public` `class` `Improve { ` ` `  `    ``// function to convert ` `    ``// Hexadecimal to Binary Number ` `    ``static` `void` `HexToBin(``char` `hexdec[]) ` `    ``{ ` `        ``int` `i = ``0``; ` ` `  `        ``while` `(hexdec[i] != ``'\u0000'``) { ` ` `  `            ``switch` `(hexdec[i]) { ` `            ``case` `'0'``: ` `                ``System.out.print(``"0000"``); ` `                ``break``; ` `            ``case` `'1'``: ` `                ``System.out.print(``"0001"``); ` `                ``break``; ` `            ``case` `'2'``: ` `                ``System.out.print(``"0010"``); ` `                ``break``; ` `            ``case` `'3'``: ` `                ``System.out.print(``"0011"``); ` `                ``break``; ` `            ``case` `'4'``: ` `                ``System.out.print(``"0100"``); ` `                ``break``; ` `            ``case` `'5'``: ` `                ``System.out.print(``"0101"``); ` `                ``break``; ` `            ``case` `'6'``: ` `                ``System.out.print(``"0110"``); ` `                ``break``; ` `            ``case` `'7'``: ` `                ``System.out.print(``"0111"``); ` `                ``break``; ` `            ``case` `'8'``: ` `                ``System.out.print(``"1000"``); ` `                ``break``; ` `            ``case` `'9'``: ` `                ``System.out.print(``"1001"``); ` `                ``break``; ` `            ``case` `'A'``: ` `            ``case` `'a'``: ` `                ``System.out.print(``"1010"``); ` `                ``break``; ` `            ``case` `'B'``: ` `            ``case` `'b'``: ` `                ``System.out.print(``"1011"``); ` `                ``break``; ` `            ``case` `'C'``: ` `            ``case` `'c'``: ` `                ``System.out.print(``"1100"``); ` `                ``break``; ` `            ``case` `'D'``: ` `            ``case` `'d'``: ` `                ``System.out.print(``"1101"``); ` `                ``break``; ` `            ``case` `'E'``: ` `            ``case` `'e'``: ` `                ``System.out.print(``"1110"``); ` `                ``break``; ` `            ``case` `'F'``: ` `            ``case` `'f'``: ` `                ``System.out.print(``"1111"``); ` `                ``break``; ` `            ``default``: ` `                ``System.out.print(``"\nInvalid hexadecimal digit "` `+ hexdec[i]); ` `            ``} ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``// Get the Hexadecimal number ` `        ``String s = ``"1AC5"` `; ` `        ``char` `hexdec[] = ``new` `char``[``100``] ; ` `            ``hexdec = s.toCharArray() ; ` ` `  `        ``// Convert HexaDecimal to Binary ` `        ``System.out.print(``"\nEquivalent Binary value is : "``); ` `        ``try``{ ` `            ``HexToBin(hexdec); ` `        ``} ` `        ``catch` `(ArrayIndexOutOfBoundsException e){ ` `            ``System.out.print(``""``); ` `        ``} ` ` `  `    ``} ` `    ``// This code is contributed by ANKITRAI1 ` `} ` ` `

## C#

 `// C# program to convert Hexadecimal  ` `// number to Binary ` ` `  `class` `GFG  ` `{ ` ` `  `// function to convert ` `// Hexadecimal to Binary Number ` `static` `void` `HexToBin(``char``[] hexdec) ` `{ ` `int` `i = 0; ` ` `  `while` `(hexdec[i] != ``'\u0000'``)  ` `{ ` ` `  `    ``switch` `(hexdec[i])  ` `    ``{ ` `    ``case` `'0'``: ` `        ``System.Console.Write(``"0000"``); ` `        ``break``; ` `    ``case` `'1'``: ` `        ``System.Console.Write(``"0001"``); ` `        ``break``; ` `    ``case` `'2'``: ` `        ``System.Console.Write(``"0010"``); ` `        ``break``; ` `    ``case` `'3'``: ` `        ``System.Console.Write(``"0011"``); ` `        ``break``; ` `    ``case` `'4'``: ` `        ``System.Console.Write(``"0100"``); ` `        ``break``; ` `    ``case` `'5'``: ` `        ``System.Console.Write(``"0101"``); ` `        ``break``; ` `    ``case` `'6'``: ` `        ``System.Console.Write(``"0110"``); ` `        ``break``; ` `    ``case` `'7'``: ` `        ``System.Console.Write(``"0111"``); ` `        ``break``; ` `    ``case` `'8'``: ` `        ``System.Console.Write(``"1000"``); ` `        ``break``; ` `    ``case` `'9'``: ` `        ``System.Console.Write(``"1001"``); ` `        ``break``; ` `    ``case` `'A'``: ` `    ``case` `'a'``: ` `        ``System.Console.Write(``"1010"``); ` `        ``break``; ` `    ``case` `'B'``: ` `    ``case` `'b'``: ` `        ``System.Console.Write(``"1011"``); ` `        ``break``; ` `    ``case` `'C'``: ` `    ``case` `'c'``: ` `        ``System.Console.Write(``"1100"``); ` `        ``break``; ` `    ``case` `'D'``: ` `    ``case` `'d'``: ` `        ``System.Console.Write(``"1101"``); ` `        ``break``; ` `    ``case` `'E'``: ` `    ``case` `'e'``: ` `        ``System.Console.Write(``"1110"``); ` `        ``break``; ` `    ``case` `'F'``: ` `    ``case` `'f'``: ` `        ``System.Console.Write(``"1111"``); ` `        ``break``; ` `    ``default``: ` `        ``System.Console.Write(``"\nInvalid hexadecimal digit "` `+  ` `                                                  ``hexdec[i]); ` `        ``break``; ` `    ``} ` `    ``i++; ` `} ` `} ` ` `  `// Driver code ` `static` `void` `Main() ` `{ ` `    ``// Get the Hexadecimal number ` `    ``string` `s = ``"1AC5"` `; ` `    ``char``[] hexdec = ``new` `char``[100]; ` `           ``hexdec = s.ToCharArray(); ` ` `  `    ``// Convert HexaDecimal to Binary ` `    ``System.Console.Write(``"Equivalent Binary value is : "``); ` `    ``try` `    ``{ ` `        ``HexToBin(hexdec); ` `    ``} ` `    ``catch` `(System.IndexOutOfRangeException) ` `    ``{ ` `        ``System.Console.Write(``""``); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by mits `

## PHP

Output:

```Equivalent Binary value is : 0001101011000101
```

My Personal Notes arrow_drop_up