-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detect_Hex.cpp
executable file
·127 lines (109 loc) · 2.11 KB
/
Detect_Hex.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i,n) for(ll i=1;i<=n;i++)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007
ll a[100],j=1;
void HexToBin(string hexdec)
{
ll i=0;
string l="";
while (hexdec[i]) {
switch (hexdec[i]) {
case '0':
l=l+"0000";
break;
case '1':
l=l+"0001";
break;
case '2':
l=l+"0010";
break;
case '3':
l=l+ "0011";
break;
case '4':
l=l+ "0100";
break;
case '5':
l=l+"0101";
break;
case '6':
l=l+"0110";
break;
case '7':
l=l+"0111";
break;
case '8':
l=l+"1000";
break;
case '9':
l=l+"1001";
break;
case 'A':
case 'a':
l=l+"1010";
break;
case 'B':
case 'b':
l=l+"1011";
break;
case 'C':
case 'c':
l=l+"1100";
break;
case 'D':
case 'd':
l=l+"1101";
break;
case 'E':
case 'e':
l=l+"1110";
break;
case 'F':
case 'f':
l=l+"1111";
break;
default:
cout << "\nInvalid hexadecimal digit "
<< hexdec[i];
}
i++;
}
return l;
}
void toInt(string l)
{
for(ll k=0;k<l.length();k++)
{
if(l[k]=='1')
a[j]=1;
j++;
}
}
ll countBits(ll number)
{
return (ll)log2(number)+1;
}
int main()
{
ll n; string s;
cout<<"Enter the code: ";
cin>>s;
string x=HexToBin(s); // converting the code
toInt(x); // to Integer array
n=j-1;
ll r=countBits(n);
ll ans,p=0;
f(k,r)
{
ans=0;
f(i,n)
if (i & (1 << (k - 1)))
ans=ans^a[i];
if(ans)
p=p+(1<<(k-1));
}
cout<<"\nError at position "<<p;
}