C++ Exercises: Convert hexadecimal number to binary number
C++ For Loop: Exercise-80 with Solution
Write a C++ program to convert a hexadecimal number to a binary number.
Visual Presentation:
Sample Solution:-
C++ Code :
#include<iostream> // Include necessary libraries
#include<stdlib.h>
#include<math.h>
using namespace std; // Use the standard namespace
// Function to convert hexadecimal to decimal
unsigned long Hex_To_Dec(char hex[]) {
char *hexstring;
int length = 0;
const int hexbase = 16; // Define base 16 for hexadecimal
unsigned long dnum = 0;
int i;
// Loop to find the length of the hexadecimal string
for (hexstring = hex; *hexstring != '\0'; hexstring++) {
length++;
}
hexstring = hex; // Reset the pointer to the start of the hexadecimal string
// Loop to convert each character of the hexadecimal string to decimal
for (i = 0; *hexstring != '\0' && i < length; i++, hexstring++) {
if (*hexstring >= 48 && *hexstring <= 57) {
// Convert character to decimal if it's a number (0-9)
dnum += (((int)(*hexstring)) - 48) * pow(hexbase, length - i - 1);
}
else if ((*hexstring >= 65 && *hexstring <= 70)) {
// Convert character to decimal if it's an uppercase letter (A-F)
dnum += (((int)(*hexstring)) - 55) * pow(hexbase, length - i - 1);
}
else if (*hexstring >= 97 && *hexstring <= 102) {
// Convert character to decimal if it's a lowercase letter (a-f)
dnum += (((int)(*hexstring)) - 87) * pow(hexbase, length - i - 1);
}
else {
// Display an error message for an invalid input character
cout << " The given hexadecimal number is invalid. \n";
}
}
return dnum; // Return the resulting decimal number
}
// Main function
int main() {
unsigned long dnum; // Variable to store the decimal number
char hex[9]; // Array to store the input hexadecimal number
int dec_num, rem = 1, m, n;
int bin_num[100], quot;
dec_num = 0;
cout << "\n\n Convert any hexadecimal number to binary number:\n"; // Display purpose of the program
cout << "------------------------------------------------------\n"; // Display separator line
cout << " Input any 32-bit Hexadecimal Number: ";
cin >> hex; // Read the hexadecimal input from the user
dnum = Hex_To_Dec(hex); // Call the function to convert hexadecimal to decimal
quot = dnum;
cout << " The equivalent binary number is: "; // Displaying the result in binary
// Conversion from decimal to binary
while (quot != 0) {
bin_num[m++] = quot % 2;
quot = quot / 2;
}
for (n = m - 1; n >= 0; n--) {
dec_num = (dec_num * 10) + bin_num[n];
}
cout << dec_num << endl; // Display the resulting binary number
cout << endl;
}
Sample Output:
Convert any hexadecimal number to binary number: ------------------------------------------------------ Input any 32-bit Hexadecimal Number: 5f The equivalant binary number is: 1011111
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C++ to convert a hexadecimal number to decimal number.
Next: Write a program in C++ to convert a hexadecimal number to octal number.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/cpp-exercises/for-loop/cpp-for-loop-exercise-80.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics