-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareBinaryToHexa.cpp
More file actions
152 lines (118 loc) · 3.11 KB
/
CompareBinaryToHexa.cpp
File metadata and controls
152 lines (118 loc) · 3.11 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Objective:
Write a function to check if the value of a binary number (passed as a string) equals the hexadecimal representation of a string.
The numbers 0000000111010010 and 01D2 are Equal
*/
#include <iostream>
#include <map>
using namespace std;
// To execute C++, please define "int main()"
map<string,unsigned int> binaryMap;
map<string,unsigned int> hexadecimalMap;
void MapInit()
{
//binary map
binaryMap["0000"] = 0x0;
binaryMap["0001"] = 0x1;
binaryMap["0010"] = 0x2;
binaryMap["0011"] = 0x3;
binaryMap["0100"] = 0x4;
binaryMap["0101"] = 0x5;
binaryMap["0110"] = 0x6;
binaryMap["0111"] = 0x7;
binaryMap["1000"] = 0x8;
binaryMap["1001"] = 0x9;
binaryMap["1010"] = 0xA;
binaryMap["1011"] = 0xB;
binaryMap["1100"] = 0xC;
binaryMap["1101"] = 0xD;
binaryMap["1110"] = 0xE;
binaryMap["1111"] = 0xF;
//hexadecimal map
hexadecimalMap["0"] = 0x0;
hexadecimalMap["1"] = 0x1;
hexadecimalMap["2"] = 0x2;
hexadecimalMap["3"] = 0x3;
hexadecimalMap["4"] = 0x4;
hexadecimalMap["5"] = 0x5;
hexadecimalMap["6"] = 0x6;
hexadecimalMap["7"] = 0x7;
hexadecimalMap["8"] = 0x8;
hexadecimalMap["9"] = 0x9;
hexadecimalMap["A"] = 0xA;
hexadecimalMap["B"] = 0xB;
hexadecimalMap["C"] = 0xC;
hexadecimalMap["D"] = 0xD;
hexadecimalMap["E"] = 0xE;
hexadecimalMap["F"] = 0xF;
}
unsigned int Binary2Int(string sBinaryNumber)
{
if(binaryMap.find(sBinaryNumber) != binaryMap.end())
{
cout<<"[Binary2Int] number "<<sBinaryNumber<<" found in map"<<endl;
return binaryMap[sBinaryNumber];
}
cout<<"[Binary2Int] Throwing exception"<<endl;
throw (exception());
}
unsigned int Hexadecimal2Int(string sHexadecimalNumber)
{
if(hexadecimalMap.find(sHexadecimalNumber) != hexadecimalMap.end())
{
cout<<"[Hexadecimal2Int] number "<<sHexadecimalNumber<<" found in map"<<endl;
return hexadecimalMap[sHexadecimalNumber];
}
cout<<"[Hexadecimal2Int] Throwing exception"<<endl;
throw (exception());
}
bool AreEqual(string sBinary, string sHexadecimal)
{
int i = sBinary.size();
int j = sHexadecimal.size();
string sBinaryNumber, sHexadecimalNumber;
if( i == 0 || j == 0 )
{
cout<<"[AreEqual] Throwing exception";
throw(exception());
}
while( i > 0 && j > 0 )
{
//extract the numbers
sBinaryNumber = sBinary.substr( i - 4, 4 );
sHexadecimalNumber = sHexadecimal.substr( j - 1, 1 );
try
{
if(Binary2Int(sBinaryNumber) != Hexadecimal2Int(sHexadecimalNumber))
{
return false;
}
}
catch(exception &ex)
{
throw(ex);
}
i-=4; j-=1;
}
if( i > 0 || j > 0 )
{
return false;
}
return true;
}
int main() {
cout<<"Objective: Write a function to check if the value of a binary number"<<endl;
cout<<"(passed as a string) equals the hexadecimal representation of a string."<<endl<<endl;
string A = "0000000111010010";
string B = "01D2";
MapInit();
try
{
cout<<"The numbers "<<A<<" and "<<B<<" are "<<(AreEqual(A, B)?"Equal":"Not Equal");
}
catch(exception &ex)
{
cout<<"Exception: Invalid format - "<<ex.what();
}
return 0;
}