This is a C++ Program to Convert a HexadecimalNumber to its Binary Equivalent. Before that, you must have knowledge of the following topics in C++.
Binary number
The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.
Hexadecimal number
The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.
Now let us go through a program for hexadecimal to Binary conversion in C++.
C++ Program to Convert Hexadecimal to Binary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include<iostream> using namespace std; void conversionFunc(string); //main function int main() { string hexDeci; cout << "Enter the Hexadecimal Number: "; cin >> hexDeci; cout << "Equivalent Binary value: " ; conversionFunc(hexDeci); return 0; } //user-defined function void conversionFunc(string hexDeci) { int i = 0; while (hexDeci[i]) { switch (hexDeci[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 << "Invalid HexaDecimal Number----"<< hexDeci[i]; } i++; } } |
Output:
//Run 1
Enter the Hexadecimal Number: A
Equivalent Binary value: 1010
//Run 2
Enter the Hexadecimal Number: 2A
Equivalent Binary value: 00101010
You may go through the vice-versa program: