-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
53 lines (45 loc) · 1.29 KB
/
utils.cpp
File metadata and controls
53 lines (45 loc) · 1.29 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
//
// Created by pengx on 2025/5/6.
//
#include "utils.hpp"
#include <QDebug>
#include <QRegularExpression>
#include <QTextCodec>
QString Utils::formatByteArray(const QByteArray &data) {
const QString hex = data.toHex().toUpper();
QString hexWithSpaces;
for (int i = 0; i < hex.length(); i += 2) {
hexWithSpaces += hex.mid(i, 2) + " ";
}
if (!hexWithSpaces.isEmpty()) {
hexWithSpaces.chop(1);
}
return hexWithSpaces;
}
QByteArray Utils::formatHexString(const QString &command) {
QByteArray byteArray;
QStringList hexList = command.split(' ', QString::SkipEmptyParts);
// 使用 const 引用来避免复制
const QStringList &listRef = hexList;
for (const QString &hex : listRef) {
bool ok;
const uint value = hex.toUInt(&ok, 16);
if (ok) {
byteArray.append(static_cast<char>(value));
}
}
return byteArray;
}
bool Utils::isHexString(const QString &command) {
// 移除所有空格
QString cleanedStr = command;
cleanedStr.remove(' ');
// 使用正则表达式检查是否为有效的十六进制字符串
static const QRegularExpression hexPattern("^[0-9a-fA-F]+$");
return hexPattern.match(cleanedStr).hasMatch();
}
bool Utils::isPositiveInt(const QString &str) {
bool ok;
const int value = str.toInt(&ok);
return ok && value > 0;
}