C++ Examples

WAP in C++ to Convert Hexadecimal number to Binary number

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X