-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_tools.cpp
More file actions
67 lines (62 loc) · 1.51 KB
/
type_tools.cpp
File metadata and controls
67 lines (62 loc) · 1.51 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
#include "type_tools.h"
/*
int num = int((unsigned char)(*(bytes + 3)) << 24 |
(unsigned char)(*(bytes + 2)) << 16 |
(unsigned char)(*(bytes + 1)) << 8 |
(unsigned char)(*(bytes + 0)));
return num;
*/
int CharBcd2Int(int dec)
{
int i = 0;
u_int8_t res[8];
int des = 0;
while (dec)
{
res[i] = (u_int8_t)dec % 16;
dec /= 16;
i++;
}
while (i)
{
i--;
des += (int)res[i] * (int)(pow(10.0, (double)i));
}
return des;
}
int Bcd2Stamp(int y1, int y2, int m, int d, int H, int M, int S)
{
struct tm tm;
tm.tm_year = (CharBcd2Int(y1) - 19) * 100 + CharBcd2Int(y2);
tm.tm_mon = CharBcd2Int(m);
tm.tm_mday = CharBcd2Int(d);
tm.tm_hour = CharBcd2Int(H);
tm.tm_min = CharBcd2Int(M);
tm.tm_sec = CharBcd2Int(S);
return (int)mktime(&tm);
}
int Hex2Ascii(const char* hex, unsigned char* ascii)
{
int len = (int)strlen(hex), tlen, i, cnt;
for (i = 0, cnt = 0, tlen = 0; i < len; i++)
{
char c = (char)toupper(hex[i]);
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))
{
char t = (c >= 'A') ? (char)(c - 'A' + 10) : (char)(c - '0');
if (cnt)
{
ascii[tlen] = (unsigned char)(ascii[tlen] + t);
tlen++;
//tlen++;
cnt = 0;
}
else
{
ascii[tlen] = (unsigned char)(t << 4);
cnt = 1;
}
}
}
return tlen;
}