-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.cpp
68 lines (56 loc) · 1.3 KB
/
decoder.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 <iostream>
#include <vector>
int main(int argc, char **argvx) {
int mdepth = atoi(argvx[1]);
std::cout << "mdepth: " << mdepth << std::endl;
float cur, prev;
std::cin >> prev;
int cnt = 0;
int state = 1;
float threshold = 1.5;
std::vector<int> vec;
for (int i = 0; i < mdepth - 1; i++) {
std::cin >> cur;
// std::cout << cur << std::endl;
cnt++;
if (cur < threshold && state == 1) {
vec.push_back(cnt);
cnt = 0;
state = 0;
} else if (threshold < cur && state == 0) {
vec.push_back(cnt);
cnt = 0;
state = 1;
}
}
if (!(vec.size() > 6 && vec.size() % 2 == 0)) {
std::cerr << "failed total length: " << vec.size() << std::endl;
return 1;
}
std::vector<unsigned char> bits;
for (int i = 3; i < vec.size() - 2; i += 2) {
if (vec[i + 1] > vec[i] * 1.5) {
bits.push_back(1);
} else {
bits.push_back(0);
}
}
if (bits.size() % 8 != 0) {
std::cerr << "failed 8 bits" << std::endl;
return 1;
}
std::vector<unsigned char> data;
for (int i = 0; i < bits.size() / 8; i++) {
unsigned char d = 0;
for (int j = 7; j >= 0; j--) {
d <<= 1;
d |= bits[i * 8 + j];
}
data.push_back(d);
}
for (auto d : data) {
printf("%02x ", d);
}
printf("\n");
return 0;
}