檔案CheckSum累加和校驗計算
阿新 • • 發佈:2019-02-07
檔案累加和校驗計算
#include"mainwindow.h"
#include"ui_mainwindow.h"
#include<QDebug>
MainWindow::MainWindow(QWidget*parent):
QMainWindow(parent),
ui(newUi::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
deleteui;
}
voidMainWindow::on_action_O_triggered()
{
filename=QFileDialog::getOpenFileName(this,
tr("OpenBinFile"),".",
tr("Binfiles(*.bin)"));
if(!filename.isEmpty())
ui->lineEdit->setText(filename);
}
intMainWindow::CHECKSUM(QByteArraybuf)
{
intsum=0;
for(inti=0;i<buf.size();i++)
{
sum+=buf.at(i)&0x000000FF;
}
returnsum;
}
voidMainWindow::on_pushButton_Open_clicked()
{
on_action_T_triggered();
}
voidMainWindow::on_action_T_triggered()
{
QStringscrFile=ui->lineEdit->text();
//讀二進位制檔案
QFilerfile(scrFile);
if(!rfile.open(QIODevice::ReadOnly))
{
QMessageBox::critical(this,"Warning","Can'topenfileforreading",QMessageBox::Ok);
rfile.close();
return;
}
intsum=CHECKSUM(rfile.readAll());
QStringtemp2=QString::number(sum,16);
ui->lineEdit_2->setText("0x"+temp2);
rfile.close();
}
包含標頭檔案:
#ifndefMAINWINDOW_H
#defineMAINWINDOW_H
#include<QMainWindow>
#include<QFileDialog>
#include<QMessageBox>
namespaceUi{
classMainWindow;
}
classMainWindow:publicQMainWindow
{
Q_OBJECT
public:
explicitMainWindow(QWidget*parent=0);
~MainWindow();
intCHECKSUM(QByteArraybuf);
privateslots:
voidon_action_O_triggered();
voidon_pushButton_Open_clicked();
voidon_action_T_triggered();
private:
Ui::MainWindow*ui;
QLabel*label;
QImageimage;
QStringfilename;
};
#endif//MAINWINDOW_H