-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.cpp
More file actions
113 lines (104 loc) · 2.2 KB
/
Copy pathCommon.cpp
File metadata and controls
113 lines (104 loc) · 2.2 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
#include "Common.hpp"
#include <sys/time.h>
static const char* const lut = "0123456789ABCDEF";
Common::Common()
{
}
uint8_t Common::hexToDecUnit(const char& c, bool &ok)
{
if(c<48)
{
ok=false;
return 0;
}
if(c<=57)
{
ok=true;
return c-48;
}
if(c<65)
{
ok=false;
return 0;
}
if(c<=70)
{
ok=true;
return c-65+10;
}
if(c<97)
{
ok=false;
return 0;
}
if(c<=102)
{
ok=true;
return c-(uint8_t)97+10;
}
ok=false;
return 0;
}
std::string Common::hexaToBinary(const std::string &hexa)
{
if(hexa.size()%2!=0)
return std::string();
std::string r;
r.resize(hexa.size()/2);
unsigned int index=0;
while(index<r.size())
{
bool ok=true;
const uint8_t c1=hexToDecUnit(hexa.at(index*2),ok);
if(!ok)
return std::string();
const uint8_t c2=hexToDecUnit(hexa.at(index*2+1),ok);
if(!ok)
return std::string();
r[index]=c1*16+c2;
index++;
}
return r;
}
uint64_t Common::hexaTo64Bits(const std::string &hexa)
{
char * pEnd=nullptr;
const char * d=hexa.c_str();
return strtoull(d,&pEnd,16);
}
std::string Common::binarytoHexa(const char * const data, const uint32_t &size)
{
std::string output;
//output.reserve(2*size);
for(size_t i=0;i<size;++i)
{
const unsigned char c = data[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
void Common::binarytoHexaC64Bits(const char * const source, char * const destination)
{
for(size_t i=0;i<8;++i)
{
const unsigned char c = source[i];
destination[i*2]=lut[c >> 4];
destination[i*2+1]=lut[c & 15];
}
}
void Common::binarytoHexaC32Bits(const char * const source, char * const destination)
{
for(size_t i=0;i<4;++i)
{
const unsigned char c = source[i];
destination[i*2]=lut[c >> 4];
destination[i*2+1]=lut[c & 15];
}
}
uint64_t Common::msFrom1970() //ms from 1970
{
struct timeval te;
gettimeofday(&te, NULL);
return te.tv_sec*1000LL + te.tv_usec/1000;
}