forked from martinjonas/Q3B
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexHelper.h
43 lines (40 loc) · 1.12 KB
/
HexHelper.h
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
#ifndef HEXHELPER_H
#define HEXHELPER_H
class HexHelper
{
private:
static const char* hex_char_to_bin(char c)
{
// TODO handle default / error
switch(toupper(c))
{
case '0': return "0000";
case '1': return "0001";
case '2': return "0010";
case '3': return "0011";
case '4': return "0100";
case '5': return "0101";
case '6': return "0110";
case '7': return "0111";
case '8': return "1000";
case '9': return "1001";
case 'A': return "1010";
case 'B': return "1011";
case 'C': return "1100";
case 'D': return "1101";
case 'E': return "1110";
case 'F': return "1111";
default: return "0000";
}
}
public:
static std::string hex_str_to_bin_str(const std::string& hex)
{
// TODO use a loop from <algorithm> or smth
std::string bin;
for(unsigned i = 0; i != hex.length(); ++i)
bin += hex_char_to_bin(hex[i]);
return bin;
}
};
#endif // HEXHELPER_H