forked from KTZ-Goel/PacketSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.cpp
executable file
·123 lines (106 loc) · 2.61 KB
/
shared.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
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
114
115
116
117
118
119
120
121
122
123
#include <stdint.h>
#include <stdio.h>
#include "shared.h"
void printBinaryuint8_t(uint8_t byte)
{
uint8_t mask = 0x80; // 1000 0000
// 打印出“byte”的二进制表示形式,没有换行符
while (mask > 0) {
putchar((byte & mask) ? '1' : '0');
mask >>= 1;
}
}
// 打印出“ byte”的二进制表示形式,没有换行符
void printBinaryuint16_t(uint16_t byte2)
{
uint16_t mask = 0x8000; // 1000 0000 0000 0000
uint8_t n = 1; // 在每4位之间放置一个空格的计数器
while (mask > 0) {
putchar((byte2 & mask) ? '1' : '0');
mask >>= 1;
if ((n & 0x03) == 0) { // (n & 0x03) is equivelant to (n % 4)
putchar(' ');
}
n++;
}
}
//打印出short的二进制表示形式,
//但为值<开始和值>结束打印点
//用于显示uint16_t中的当前标志
// For example:
// byte2 ----- 10010001 00001101
// start ----- 3
// end ------- 6;
// printed --- "...1000.........", 没有引号,没有换行符
void printBinaryuint16_tdots(uint16_t byte2, int start, int end)
{
int i = 0;
while (i < start) {
putchar('.');
i++;
}
uint16_t mask = 0x8000;
mask >>= start;
while (i <= end) {
putchar((byte2 & mask) ? '1' : '0');
mask >>= 1;
i++;
}
while (i <= 15) {
putchar('.');
i++;
}
}
const char* strBinaryuint16_tdots(uint16_t byte2, int start, int end)
{
static char buffer[17];
int i = 0;
while (i < start) {
buffer[i] = '.';
i++;
}
uint16_t mask = 0x8000;
mask >>= start;
while (i <= end) {
buffer[i] = (byte2 & mask) ? '1' : '0';
mask >>= 1;
i++;
}
while (i <= 15) {
buffer[i] = '.';
i++;
}
buffer[16] = '\0';
return buffer;
}
const char* strBinaryuint8_t(uint8_t byte)
{
static char buffer[9];
uint8_t mask = 0x80; // 1000 0000
// 将“byte”的二进制表示形式转换为字符串
int i = 0;
while (mask > 0) {
buffer[i] = (byte & mask) ? '1' : '0';
i++;
mask >>= 1;
}
buffer[8] = '\0';
return buffer;
}
void setBackgroundColor(QList<QStandardItem*>* row, QColor color)
{
for (int i = 0; i < row->length(); ++i) {
row->at(i)->setData(color, Qt::BackgroundColorRole);
}
}
QString getHTMLentity(char c)
{
QHash<char, QString> htmlEntities;
htmlEntities.insert('<', "<");
htmlEntities.insert('>', ">");
if (htmlEntities.contains(c)) {
return htmlEntities.value(c);
} else {
return QString(c);
}
}