0% found this document useful (0 votes)
53 views

Hexadecimal To Binary Conversion

This C++ program takes a hexadecimal number as input from the user, uses a switch statement to convert each hexadecimal digit to its 4-bit binary equivalent, and outputs the full binary conversion by concatenating the binary outputs for each digit. It includes header files for input/output and string handling, declares arrays to hold the input hex number and output binary number, uses a for loop and switch statement to iterate through each character of the input and perform the hex to binary digit conversion, and outputs either the binary conversion or an error message if an invalid hexadecimal digit is encountered.

Uploaded by

Arlene Polinar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Hexadecimal To Binary Conversion

This C++ program takes a hexadecimal number as input from the user, uses a switch statement to convert each hexadecimal digit to its 4-bit binary equivalent, and outputs the full binary conversion by concatenating the binary outputs for each digit. It includes header files for input/output and string handling, declares arrays to hold the input hex number and output binary number, uses a for loop and switch statement to iterate through each character of the input and perform the hex to binary digit conversion, and outputs either the binary conversion or an error message if an invalid hexadecimal digit is encountered.

Uploaded by

Arlene Polinar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// program to convert hex to binary number

#include<iostream>
#include<string.h>
using namespace std;
char hex_no[100];
char bin_no[100];
int counter;

int main()
{
cout << "Enter a hexadecimal no : ";
cin >> hex_no; // 56F
for(counter = 0; counter < strlen(hex_no); counter++)
{
switch(hex_no[counter])
{
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 hex no...";
break;
}
}

return 0;
}

You might also like