-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataencoder.cpp
More file actions
80 lines (65 loc) · 2.15 KB
/
dataencoder.cpp
File metadata and controls
80 lines (65 loc) · 2.15 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
#include "dataencoder.h"
#include <QRegExp>
DataEncoder::DataEncoder() : m_str_hex(""), m_str_bin(""), m_str_b64(""), m_str_ascii("") {}
void DataEncoder::Convert(EncodingType encodingType, QString data) {
switch (encodingType) {
case HEX:
m_str_hex = data;
break;
case BINARY:
m_str_bin = data;
m_str_hex = fromByteArrayToHex(
QByteArray::fromHex(fromByteArrayToBinary(data.toUtf8()).toUtf8()));
break;
case BASE64:
m_str_b64 = data;
m_str_hex = fromByteArrayToHex(QByteArray::fromBase64(data.toUtf8()));
break;
case ASCII:
m_str_ascii = data;
m_str_hex = fromByteArrayToHex(data.toUtf8());
break;
default:
break;
}
m_str_bin = fromByteArrayToBinary(fromHexToByteArray(m_str_hex).toHex());
m_str_b64 = QByteArray::fromHex(m_str_hex.toUtf8()).toBase64();
m_str_ascii = fromHexToByteArray(m_str_hex);
}
QString DataEncoder::str_hex() const {
return m_str_hex;
}
QString DataEncoder::str_bin() const {
return m_str_bin;
}
QString DataEncoder::str_b64() const {
return m_str_b64;
}
QString DataEncoder::str_ascii() const {
return m_str_ascii;
}
QByteArray DataEncoder::fromHexToByteArray(QString hexData) {
hexData.remove(QRegExp("\\s+"));
return QByteArray::fromHex(hexData.toUtf8());
}
QString DataEncoder::fromByteArrayToHex(QByteArray ba) {
return QString::fromUtf8(ba.toHex());
}
QString DataEncoder::fromByteArrayToBinary(QByteArray ba) {
QString binaryString = "";
for (int i = 0; i < ba.length(); i++) {
QString byteString = QString::number(ba.at(i), 2);
byteString = byteString.rightJustified(8, '0');
binaryString.append(byteString);
}
return binaryString;
}
QString DataEncoder::fromByteArrayToBase64(QByteArray ba) {
return ba.toBase64();
}
QString DataEncoder::fromHexToASCII(QString hexData) {
hexData.remove(QRegExp("\\s+"));
QByteArray bytes = QByteArray::fromHex(hexData.toUtf8());
QString asciiData = QString::fromUtf8(bytes);
return asciiData;
}