1 Star 0 Fork 1

码蚁丶/SerialWizard

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CalculateCheckSumDialog.cpp 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
//
// Created by chang on 2017-07-31.
//
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QtWidgets/QHBoxLayout>
#include <QDebug>
#include "CalculateCheckSumDialog.h"
CalculateCheckSumDialog::CalculateCheckSumDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) {
createUi();
createConnect();
}
void CalculateCheckSumDialog::createUi() {
inputLineEdit = new QLineEdit(this);
inputLineEdit->setMinimumWidth(600);
calculateButton = new QPushButton(tr("计算"), this);
xorResultLabel = new QLabel(this);
sumResultLabel = new QLabel(this);
auto inputLayout = new QHBoxLayout;
inputLayout->addWidget(inputLineEdit);
inputLayout->addWidget(calculateButton);
auto resultLayout = new QVBoxLayout;
resultLayout->addWidget(xorResultLabel);
resultLayout->addWidget(sumResultLabel);
xorResultLabel->setVisible(false);
sumResultLabel->setVisible(false);
auto layout = new QVBoxLayout;
layout->addLayout(inputLayout);
layout->addLayout(resultLayout);
layout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(layout);
setWindowTitle(tr("计算检验"));
}
void CalculateCheckSumDialog::createConnect() {
connect(calculateButton, &QPushButton::clicked, [this] {
xorResultLabel->hide();
sumResultLabel->hide();
auto input = inputLineEdit->text();
input.remove(" ");
if (input.isEmpty()) {
return;
}
QByteArray data = QByteArray();
for (int i = 0; i < input.count(); i += 2) {
auto t = input.mid(i, 2);
data.append(QByteArray::fromHex(t.toLocal8Bit()));
}
qDebug() << "calculate data:" << data.toHex();
auto sumResult = getSumResult(data);
auto xorResult = getXorResult(data);
sumResultLabel->setText(QString("%1%2").arg("检验和 ").arg(sumResult));
xorResultLabel->setText(QString("%1%2").arg("异或值 ").arg(xorResult));
xorResultLabel->show();
sumResultLabel->show();
});
}
QString CalculateCheckSumDialog::getSumResult(const QByteArray &data) {
int sum = 0x00;
for (auto s:data) {
sum += s;
}
return QString::number(sum & 0x00FFFF, 16).left(4).toUpper();
}
QString CalculateCheckSumDialog::getXorResult(const QByteArray &data) {
if (data.isEmpty()) {
return "";
}
char sum = data[0];
for (int i = 1; i < data.count(); i++) {
sum = sum ^ data[i];
}
return QString::number(sum & 0x00FF, 16).left(2).toUpper();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/tangtony/SerialWizard.git
git@gitee.com:tangtony/SerialWizard.git
tangtony
SerialWizard
SerialWizard
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385