The Program in C++ to Convert Hexadecimal number to Binary number is given below:
#include <iostream>
#include <string>
std::string hex_to_bin(std::string hex) {
std::string bin = "";
for (int i = 0; i < hex.length(); i++) {
switch (hex[i]) {
case '0': bin += "0000"; break;
case '1': bin += "0001"; break;
case '2': bin += "0010"; break;
case '3': bin += "0011"; break;
case '4': bin += "0100"; break;
case '5': bin += "0101"; break;
case '6': bin += "0110"; break;
case '7': bin += "0111"; break;
case '8': bin += "1000"; break;
case '9': bin += "1001"; break;
case 'A': bin += "1010"; break;
case 'B': bin += "1011"; break;
case 'C': bin += "1100"; break;
case 'D': bin += "1101"; break;
case 'E': bin += "1110"; break;
case 'F': bin += "1111"; break;
case 'a': bin += "1010"; break;
case 'b': bin += "1011"; break;
case 'c': bin += "1100"; break;
case 'd': bin += "1101"; break;
case 'e': bin += "1110"; break;
case 'f': bin += "1111"; break;
}
}
return bin;
}
int main() {
std::string hex;
std::cout << "Hello Codeauri Family,enter a hexadecimal number here to convert them into binary equivalent: \n";
std::cin >>hex;
std::cout << "Well, the Binary equivalent is : " << hex_to_bin(hex) << std::endl;
return 0;
}
Output:
Hello Codeauri Family,enter a hexadecimal number here to convert them into binary equivalent:
5f
Well, the Binary equivalent is : 01011111
Pro-Tips💡
This program prompts the user to enter a hexadecimal number and converts it to its equivalent binary value using a function hex_to_bin().
The function takes in a string, iterates through each character of the string, and uses a switch case statement to convert the hexadecimal digits to their binary equivalents.
The binary equivalents of each hexadecimal digit are concatenated to form the final binary number.
Finally, the binary number is returned.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.