-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
93 lines (75 loc) · 1.95 KB
/
mainwindow.cpp
File metadata and controls
93 lines (75 loc) · 1.95 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->hexLineEdit->setFocus();
}
MainWindow::~MainWindow()
{
delete ui;
}
static quint32 toUint(const QByteArray &data, bool bigEndian)
{
if (data.size() != 4)
return 0;
quint32 word = 0;
if (bigEndian) {
word = quint32((quint8(data.at(0)) << 24) |
(quint8(data.at(1)) << 16) |
(quint8(data.at(2)) << 8) |
(quint8(data.at(3)) << 0));
} else {
word = quint32((quint8(data.at(0)) << 0) |
(quint8(data.at(1)) << 8) |
(quint8(data.at(2)) << 16) |
(quint8(data.at(3)) << 24));
}
return word;
}
static float toFloat(const QString &data, bool bigEndian)
{
const QByteArray ba = QByteArray::fromHex(data.toLatin1());
quint32 word = toUint(ba, bigEndian);
const float *f = reinterpret_cast<const float *>(&word);
return *f;
}
static QString toHex(quint32 value, bool bigEndian)
{
QByteArray ba;
if (bigEndian) {
ba.append(char(value >> 24));
ba.append(char(value >> 16));
ba.append(char(value >> 8));
ba.append(char(value >> 0));
} else {
ba.append(char(value >> 0));
ba.append(char(value >> 8));
ba.append(char(value >> 16));
ba.append(char(value >> 24));
}
return ba.toHex().toUpper();
}
static QString toHex(float value, bool bigEndian)
{
const quint32 *i = reinterpret_cast<const quint32 *>(&value);
return toHex(*i, bigEndian);
}
void MainWindow::on_hexLineEdit_textEdited()
{
const bool bigEndian = ui->bigEndian->isChecked();
float f = toFloat(ui->hexLineEdit->text(), bigEndian);
ui->floatLineEdit->setText(QString::number(double(f)));
}
void MainWindow::on_floatLineEdit_textEdited()
{
const bool bigEndian = ui->bigEndian->isChecked();
const float f = ui->floatLineEdit->text().toFloat();
ui->hexLineEdit->setText(toHex(f, bigEndian));
}
void MainWindow::on_bigEndian_toggled()
{
on_floatLineEdit_textEdited();
}