-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlotools.cpp
79 lines (70 loc) · 1.62 KB
/
lotools.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
#include "lotools.h"
LoTools::LoTools(){}
// Network
QStringList LoTools::getLocalIPv4List()
{
QStringList res;
QList<QHostAddress> ip_list = QNetworkInterface::allAddresses();
foreach (QHostAddress ip, ip_list) {
if(QAbstractSocket::IPv4Protocol == ip.protocol())
res.push_back(ip.toString());
}
return res;
}
// Data Conversion
QByteArray LoTools::Hex2BArray(const QString &h)
{
if(h.isEmpty())
return QByteArray();
bool ok;
quint8 b;
QByteArray d;
QStringList b_list;
d.clear();
b_list = h.split(' ');
if(b_list.last().isEmpty())
b_list.takeLast();
foreach(QString b_str, b_list) {
b = b_str.toUInt(&ok, 16);
d.append(b);
}
return d;
}
QString LoTools::BArray2Hex(const QByteArray &a)
{
QString s;
s.clear();
if(a.isEmpty()) return s;
foreach(char b, a)
s.append(QString("%1 ").arg((quint8)b, 2, 16, QLatin1Char('0')));
s.remove(s.length()-1, 1);
return s;
}
QString LoTools::Str2Hex(const QString &s)
{
if(s.isEmpty()) return s;
QByteArray a = s.toUtf8();
return BArray2Hex(a);
}
QString LoTools::Hex2Str(const QString &h)
{
if(h.isEmpty()) return h;
return QString().fromUtf8(Hex2BArray(h));
}
// Qt Extend
void LoTools::go2TheEnd(QTextEdit *te)
{
QTextCursor cursor = te->textCursor();
cursor.movePosition(QTextCursor::End);
te->setTextCursor(cursor);
}
void LoTools::append(QTextEdit *te, const QByteArray &d)
{
te->append(d);
go2TheEnd(te);
}
void LoTools::append(QTextEdit *te, const QString &s)
{
te->append(s);
go2TheEnd(te);
}