-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexaToDeci.cpp
33 lines (28 loc) · 877 Bytes
/
HexaToDeci.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
#include <bits/stdc++.h>
using namespace std;
int HexaToDeci(string s)
{
int n = s.length();
int base = 1; // for unit place we multiply by 16^0 == 1
int deci_num = 0;
for (int i = n - 1; i >= 0; i--)
{
if (s[i] >= '0' && s[i] <= '9') // here if char is an integer betn 0 - 9 we convert it to integer
{
deci_num += (int(s[i] - 48)) * base;
base *= 16; // for tens place and so we need to update the base value i.e 16^1 = 16, 16^2...;
}
else if (s[i] >= 'A' && s[i] <= 'F') // here if char is betn A and F we convert it into their respective integer equivalent
{
deci_num += (int(s[i] - 55)) * base;
base *= 16;
}
}
return deci_num;
}
int main()
{
string s; // input of a hexadecimal number as a string
cin >> s;
cout << HexaToDeci(s);
}