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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include "xbytearray.h"
XByteArray::XByteArray()
{
_oldSize = -99;
_addressNumbers = 4;
_addressOffset = 0;
}
int XByteArray::addressOffset()
{
return _addressOffset;
}
void XByteArray::setAddressOffset(int offset)
{
_addressOffset = offset;
}
int XByteArray::addressWidth()
{
return _addressNumbers;
}
void XByteArray::setAddressWidth(int width)
{
if ((width >= 0) && (width<=6))
{
_addressNumbers = width;
}
}
QByteArray & XByteArray::data()
{
return _data;
}
void XByteArray::setData(QByteArray data)
{
_data = data;
_changedData = QByteArray(data.length(), char(0));
}
bool XByteArray::dataChanged(int i)
{
return bool(_changedData[i]);
}
QByteArray XByteArray::dataChanged(int i, int len)
{
return _changedData.mid(i, len);
}
void XByteArray::setDataChanged(int i, bool state)
{
_changedData[i] = char(state);
}
void XByteArray::setDataChanged(int i, const QByteArray & state)
{
int length = state.length();
int len;
if ((i + length) > _changedData.length())
len = _changedData.length() - i;
else
len = length;
_changedData.replace(i, len, state);
}
int XByteArray::realAddressNumbers()
{
if (_oldSize != _data.size())
{
// is addressNumbers wide enought?
QString test = QString("%1")
.arg(_data.size() + _addressOffset, _addressNumbers, 16, QChar('0'));
_realAddressNumbers = test.size();
}
return _realAddressNumbers;
}
int XByteArray::size()
{
return _data.size();
}
QByteArray & XByteArray::insert(int i, char ch)
{
_data.insert(i, ch);
_changedData.insert(i, char(1));
return _data;
}
QByteArray & XByteArray::insert(int i, const QByteArray & ba)
{
_data.insert(i, ba);
_changedData.insert(i, QByteArray(ba.length(), char(1)));
return _data;
}
QByteArray & XByteArray::remove(int i, int len)
{
_data.remove(i, len);
_changedData.remove(i, len);
return _data;
}
QByteArray & XByteArray::replace(int index, char ch)
{
_data[index] = ch;
_changedData[index] = char(1);
return _data;
}
QByteArray & XByteArray::replace(int index, const QByteArray & ba)
{
int len = ba.length();
return replace(index, len, ba);
}
QByteArray & XByteArray::replace(int index, int length, const QByteArray & ba)
{
int len;
if ((index + length) > _data.length())
len = _data.length() - index;
else
len = length;
_data.replace(index, len, ba.mid(0, len));
_changedData.replace(index, len, QByteArray(len, char(1)));
return _data;
}
QChar XByteArray::asciiChar(int index)
{
char ch = _data[index];
if ((ch < 0x20) || (ch > 0x7e))
ch = '.';
return QChar(ch);
}
QString XByteArray::toRedableString(int start, int end)
{
int adrWidth = realAddressNumbers();
if (_addressNumbers > adrWidth)
adrWidth = _addressNumbers;
if (end < 0)
end = _data.size();
QString result;
for (int i=start; i < end; i += 16)
{
QString adrStr = QString("%1").arg(_addressOffset + i, adrWidth, 16, QChar('0'));
QString hexStr;
QString ascStr;
for (int j=0; j<16; j++)
{
if ((i + j) < _data.size())
{
hexStr.append(" ").append(_data.mid(i+j, 1).toHex());
ascStr.append(asciiChar(i+j));
}
}
result += adrStr + " " + QString("%1").arg(hexStr, -48) + " " + QString("%1").arg(ascStr, -17) + "\n";
}
return result;
}
|