forked from codinasion/codinasion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertHexadecimalToBinary.cpp
More file actions
50 lines (47 loc) · 1.37 KB
/
ConvertHexadecimalToBinary.cpp
File metadata and controls
50 lines (47 loc) · 1.37 KB
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
#include <iostream>
#include <string>
#include <unordered_map>
#include <stdexcept>
using namespace std;
string hex_to_bin(string hex_num) {
// Map containing hexadecimal to binary conversion
unordered_map<char, string> hex_to_binary_map = {
{'0', "0000"},
{'1', "0001"},
{'2', "0010"},
{'3', "0011"},
{'4', "0100"},
{'5', "0101"},
{'6', "0110"},
{'7', "0111"},
{'8', "1000"},
{'9', "1001"},
{'A', "1010"},
{'B', "1011"},
{'C', "1100"},
{'D', "1101"},
{'E', "1110"},
{'F', "1111"}
};
string binary_str = "";
for (char d: hex_num) {
if (!hex_to_binary_map.count(d)) {
throw invalid_argument("Invalid hexadecimal digit: " + string(1, d));
}
binary_str = binary_str + hex_to_binary_map[d];
}
return binary_str;
}
int main() {
string hex_num;
cout << "Type a HexaDecimal number: "; // Type a number and press enter
cin >> hex_num; // Get user input from the keyboard
try {
cout << "Your Binary number is:" << endl;
cout << hex_to_bin(hex_num) << endl;
} catch (invalid_argument& e) {
cerr << e.what() << endl;
return 1;
}
return 0;
}