-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.cpp
executable file
·68 lines (65 loc) · 1.17 KB
/
utility.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
#include <bits/stdc++.h>
using namespace std;
int hex_to_dec(string s)
{
int n = s.size() - 1, pow = 0, result = 0;
for (int i = n; i >= 0; --i)
{
int mul = 1;
for (int j = 1; j <= pow; ++j)
{
mul *= 16;
}
result += char_to_int[s[i]] * mul;
++pow;
}
return result;
}
string dec_to_hex(int &number)
{
int shift_bit = 1, shifting = 8, counter = 1, dec_val = 0, mul_val = 1;
string hex_val;
for (int i = 0; i < shifting; ++i)
{
if (number & shift_bit)
dec_val += mul_val;
if (counter == 4)
{
counter = mul_val = 1;
hex_val.push_back(int_to_char[dec_val]);
dec_val = 0;
}
else
{
mul_val *= 2;
++counter;
}
shift_bit <<= 1;
}
reverse(hex_val.begin(), hex_val.end());
return hex_val;
}
string dec_to_hex16(int &number)
{
int shift_bit = 1, shifting = 16, counter = 1, dec_val = 0, mul_val = 1;
string hex_val;
for (int i = 0; i < shifting; ++i)
{
if (number & shift_bit)
dec_val += mul_val;
if (counter == 4)
{
counter = mul_val = 1;
hex_val.push_back(int_to_char[dec_val]);
dec_val = 0;
}
else
{
mul_val *= 2;
++counter;
}
shift_bit <<= 1;
}
reverse(hex_val.begin(), hex_val.end());
return hex_val;
}