diff --git a/Applications/HexEditor/HexEditor.cpp b/Applications/HexEditor/HexEditor.cpp index f982f61be2..3ea3cdc587 100644 --- a/Applications/HexEditor/HexEditor.cpp +++ b/Applications/HexEditor/HexEditor.cpp @@ -116,6 +116,29 @@ bool HexEditor::copy_selected_text_to_clipboard() return true; } +bool HexEditor::copy_selected_hex_to_clipboard_as_c_code() +{ + if (m_selection_start == -1 || m_selection_end == -1 || (m_selection_end - m_selection_start) < 0 || m_buffer.is_empty()) + return false; + + StringBuilder output_string_builder; + output_string_builder.appendf("unsigned char raw_data[%d] = {\n", (m_selection_end - m_selection_start) + 1); + output_string_builder.append(" "); + for (int i = m_selection_start, j = 1; i <= m_selection_end; i++, j++) { + output_string_builder.appendf("0x%02X", m_buffer.data()[i]); + if (i != m_selection_end) + output_string_builder.append(", "); + if ((j % 12) == 0) { + output_string_builder.append("\n"); + output_string_builder.append(" "); + } + } + output_string_builder.append("\n};\n"); + + GClipboard::the().set_data(output_string_builder.to_string()); + return true; +} + void HexEditor::set_bytes_per_row(int bytes_per_row) { m_bytes_per_row = bytes_per_row; diff --git a/Applications/HexEditor/HexEditor.h b/Applications/HexEditor/HexEditor.h index cbc6456eb8..8ca6f0afd5 100644 --- a/Applications/HexEditor/HexEditor.h +++ b/Applications/HexEditor/HexEditor.h @@ -26,6 +26,7 @@ public: bool copy_selected_text_to_clipboard(); bool copy_selected_hex_to_clipboard(); + bool copy_selected_hex_to_clipboard_as_c_code(); int bytes_per_row() const { return m_bytes_per_row; } void set_bytes_per_row(int); diff --git a/Applications/HexEditor/HexEditorWidget.cpp b/Applications/HexEditor/HexEditorWidget.cpp index b07b3184ed..5264be4795 100644 --- a/Applications/HexEditor/HexEditorWidget.cpp +++ b/Applications/HexEditor/HexEditorWidget.cpp @@ -26,12 +26,11 @@ HexEditorWidget::HexEditorWidget() m_editor = HexEditor::construct(this); m_editor->on_status_change = [this](int position, HexEditor::EditMode edit_mode, int selection_start, int selection_end) { - m_statusbar->set_text(String::format("Offset: %8X, Edit Mode: %s, Selection Start: %d, Selection End: %d, Selected Bytes: %d", - position, - edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text", - selection_start, - selection_end, - (selection_end - selection_start) + 1)); + m_statusbar->set_text(0, String::format("Offset: %8X", position)); + m_statusbar->set_text(1, String::format("Edit Mode: %s", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text")); + m_statusbar->set_text(2, String::format("Selection Start: %d", selection_start)); + m_statusbar->set_text(3, String::format("Selection End: %d", selection_end)); + m_statusbar->set_text(4, String::format("Selected Bytes: %d", (selection_end - selection_start) + 1)); }; m_editor->on_change = [this] { @@ -41,7 +40,7 @@ HexEditorWidget::HexEditorWidget() update_title(); }; - m_statusbar = GStatusBar::construct(this); + m_statusbar = GStatusBar::construct(5, this); m_open_action = GCommonActions::make_open_action([this](auto&) { Optional open_path = GFilePicker::get_open_filepath(); @@ -131,6 +130,10 @@ HexEditorWidget::HexEditorWidget() edit_menu->add_action(GAction::create("Copy Text", [&](const GAction&) { m_editor->copy_selected_text_to_clipboard(); })); + edit_menu->add_separator(); + edit_menu->add_action(GAction::create("Copy As C Code", [&](const GAction&) { + m_editor->copy_selected_hex_to_clipboard_as_c_code(); + })); menubar->add_menu(move(edit_menu)); auto view_menu = make("View");