-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
72 lines (64 loc) · 2.04 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
setWindowFlags(Qt::WindowCloseButtonHint);
ui->setupUi(this);
this->setFixedSize(this->size());
processChangedText = true;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_txtHex_textChanged()
{
if(!processChangedText)
return;
static bool bProcess = true;
if ((0 == ui->txtHex->toPlainText().length()) ||
(0 != ui->txtHex->toPlainText().length()%2) ||
!bProcess) {
return;
}
processChangedText = bProcess = false;
QTextCursor textCursor = ui->txtHex->textCursor();
textCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, textCursor.position());
QString str = ui->txtHex->toPlainText();
str.replace(" ","");
ui->txtHex->setPlainText(str);
str.clear();
str = ui->txtHex->toPlainText();
QString asciiStr;
char asciiText[3]; asciiText[2] = '\0';
for (int i = 0; i < ui->txtHex->toPlainText().length(); i+=2) {
asciiText[0] = str.at(i).toLatin1();
asciiText[1] = str.at(i+1).toLatin1();
char c = strtol(asciiText, NULL, 16);
asciiStr.append(c);
}
ui->txtASCII->setPlainText(asciiStr);
ui->txtHex->setTextCursor(textCursor);
processChangedText = bProcess = true;
}
void MainWindow::on_txtASCII_textChanged()
{
static bool bProcess = true;
if(!processChangedText || !bProcess)
return;
processChangedText = bProcess = false;
QTextCursor textCursor = ui->txtASCII->textCursor();
textCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, textCursor.position());
QString str = ui->txtASCII->toPlainText();
QString hexStr;
for(int i = 0; i < str.length(); i++) {
char c = str.at(i).toLatin1();
hexStr.append(QString::number(c));
}
ui->txtHex->setPlainText(hexStr);
ui->txtASCII->setTextCursor(textCursor);
processChangedText = bProcess = true;
}