Skip to content

Commit da16abd

Browse files
committed
Automatically add spaces between bytes in hex mode
1 parent 0cef118 commit da16abd

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

assets/qml/Windows/Console.qml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,22 @@ Control {
222222
// Validate hex strings
223223
//
224224
validator: RegExpValidator {
225-
regExp: hexCheckbox.checked ? /^[a-fA-F0-9]+$/ : /[\s\S]*/
225+
regExp: hexCheckbox.checked ? /^(?:([a-f0-9]{2})\s*)+$/i : /[\s\S]*/
226226
}
227227

228228
//
229229
// Send data on <enter>
230230
//
231231
Keys.onReturnPressed: root.sendData()
232232

233+
//
234+
// Add space automatically in hex view
235+
//
236+
onTextChanged: {
237+
if (hexCheckbox.checked)
238+
send.text = Cpp_IO_Console.formatUserHex(send.text)
239+
}
240+
233241
//
234242
// Navigate command history upwards with <up>
235243
//

src/IO/Console.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,32 @@ QStringList Console::displayModes() const
197197
return list;
198198
}
199199

200+
/**
201+
* Validates the given @a text and adds space to display the text in a byte-oriented view
202+
*/
203+
QString Console::formatUserHex(const QString &text)
204+
{
205+
// Remove spaces & stuff
206+
auto data = text.simplified();
207+
data = data.replace(" ", "");
208+
209+
// Convert to hex string with spaces between bytes
210+
QString str;
211+
for (int i = 0; i < data.length(); ++i)
212+
{
213+
str.append(data.at(i));
214+
if ((i + 1) % 2 == 0 && i > 0)
215+
str.append(" ");
216+
}
217+
218+
// Chop last space
219+
while (str.endsWith(" "))
220+
str.chop(1);
221+
222+
// Return string
223+
return str;
224+
}
225+
200226
/**
201227
* Allows the user to export the information displayed on the console
202228
*/

src/IO/Console.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class Console : public QObject
115115
Q_INVOKABLE QStringList dataModes() const;
116116
Q_INVOKABLE QStringList lineEndings() const;
117117
Q_INVOKABLE QStringList displayModes() const;
118+
Q_INVOKABLE QString formatUserHex(const QString &text);
118119

119120
public slots:
120121
void save();

0 commit comments

Comments
 (0)