how to display array of hex bytes into control and translate to ascii Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
seeek
In need of some credit
In need of some credit
Posts: 7
Joined: Tue Sep 29, 2020 1:42 pm

how to display array of hex bytes into control and translate to ascii

Post by seeek » Tue Sep 29, 2020 2:02 pm

Hello that's my first topic on wx forums .



I want to print this buffer into any control like the picture .
and translate that buffer to ASCII code .
please provide any full example to study ,, because I am still beginner in wx world ;D
all I want
(a Control with hex buffer and other with the translation same as Attachment image .. )

please if your going to help me provide the same buffer as here :-

Code: Select all

int* mybuffer[]={0x40,0x00,0x83,0x08,0x08,0x80,0xFE,0xFF,0x07,0x10,0xD0,0x0F,0x18,0x00,0x20,0xEE,0x0C,0x30,0x00,0x38,0x00,0x40,0x01,0x48,0x00,0x50,0xE8,0x07,0x5A,0x00,0x72,0x06,0x53,0x59,0x53,0x54,0x45,0x4D,0x72,0x06,0x6D,0x65,0x64,0x6F,0x5F,0x6F,0x72,0x00,0x72,0x0E,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5F,0x31,0x30,0x31,0x30,0x40,0x40}
you can translate these row bytes to ascii from any webiste ..
40 00 83 08 08 80 FE FF 07 10 D0 0F 18 00 20 EE 0C 30 00 38 00 40 01 48 00 50 E8 07 5A 00 72 06 53 59 53 54 45 4D 72 06 6D 65 64 6F 5F 6F 72 00 72 0E 31 30 30 30 30 30 30 5F 31 30 31 30 40 40
Attachments
Capture.PNG

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 361
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: how to display array of hex bytes into control and translate to ascii

Post by New Pagodi » Tue Sep 29, 2020 3:35 pm

Here's a small example to dump the contents of a file to a wxStyledTextCtrl in hex form. Maybe you can adapt it for what you want to do.

Code: Select all

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include <wx/filepicker.h>
#include <wx/stc/stc.h>

class MyFrame: public wxFrame
{
    public:
        MyFrame();

    private:
        void OnFile(wxFileDirPickerEvent& event);
        void DumpBufferToStc(unsigned char*,size_t);

        wxStyledTextCtrl* m_stc;
};

MyFrame::MyFrame()
        :wxFrame(NULL, wxID_ANY, "AUI Tab", wxDefaultPosition, wxSize(600, 400))
{
    wxPanel* panel = new wxPanel(this, wxID_ANY);

    wxFilePickerCtrl* filePicker
        = new wxFilePickerCtrl(panel, wxID_ANY, wxEmptyString,
                               "Select a file", "*.*");

    m_stc = new wxStyledTextCtrl( panel, wxID_ANY);

    m_stc->SetMarginWidth(0,0);
    m_stc->SetMarginWidth(1,0);
    m_stc->SetMarginWidth(2,0);

    m_stc->StyleSetFaceName(wxSTC_STYLE_DEFAULT,"Courier New");
    m_stc->StyleSetSize(wxSTC_STYLE_DEFAULT,10);

    m_stc->StyleSetFaceName(wxSTC_STYLE_DEFAULT+1,"Courier New");
    m_stc->StyleSetSize(wxSTC_STYLE_DEFAULT+1,10);
    m_stc->StyleSetForeground(wxSTC_STYLE_DEFAULT+1,wxColor(0,0,128));
    m_stc->StyleSetBackground(wxSTC_STYLE_DEFAULT+1,wxColor(255,255,255));
    m_stc->SetScrollWidth(100);
    m_stc->SetScrollWidthTracking(true);

    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(filePicker,wxSizerFlags(0).Expand().Border(wxALL));
    sizer->Add(m_stc,wxSizerFlags(1).Expand().Border(wxLEFT|wxRIGHT|wxBOTTOM));

    panel->SetSizer(sizer);
    panel->Layout();

    filePicker->Bind(wxEVT_COMMAND_FILEPICKER_CHANGED, &MyFrame::OnFile, this);
}

unsigned char HexToAscii(unsigned char c)
{
    unsigned char val;
    switch ( c )
    {
        case 0:
            val = '0';
            break;
        case 1:
            val = '1';
            break;
        case 2:
            val = '2';
            break;
        case 3:
            val = '3';
            break;
        case 4:
            val = '4';
            break;
        case 5:
            val = '5';
            break;
        case 6:
            val = '6';
            break;
        case 7:
            val = '7';
            break;
        case 8:
            val = '8';
            break;
        case 9:
            val = '9';
            break;
        case 10:
            val = 'A';
            break;
        case 11:
            val = 'B';
            break;
        case 12:
            val = 'C';
            break;
        case 13:
            val = 'D';
            break;
        case 14:
            val = 'E';
            break;
        case 15:
            val = 'F';
            break;
        default:
            val = ' ';
            break;
    }
    return val;
}

typedef struct LineData
{
    int addressChars;
    int hexPadChars;
    int hexAreaChars;
    int asciiPadChars;
    int asciiAreaChars;
    int totalCharsToAscii;
    int totalLineChars;
    int totalLineCharsWithNewLine;

    LineData(wxFileOffset fileSize)
    {
        addressChars = fileSize < 0xFFFFFFFF?8:16;
        hexPadChars = 2;
        hexAreaChars = 16 * 3;
        asciiPadChars = 1;
        asciiAreaChars = 16;
        totalCharsToAscii = addressChars + hexPadChars +
                            hexAreaChars + asciiPadChars;
        totalLineChars = totalCharsToAscii + asciiAreaChars;
        totalLineCharsWithNewLine = totalLineChars + 1;
    }
} LineData;

void PrintLine(unsigned char* buffer, unsigned char* hexbuffer,
               int lineNo, int charsInLine, LineData* lineData)
{
    unsigned char* hexLineBuffer = hexbuffer + lineNo*(lineData->totalLineCharsWithNewLine);
    unsigned char* lineBuffer = buffer + lineNo*16;

    // Set the location and follow it with a colon and a space:
    // __________________
    // 0000000180001000: 00 00 00 00 78 02 35 5E 00 00 00 00 02 00 00 00  ....x.5^........

    if ( lineData->addressChars == 16 )
    {
        wxString s = wxString::Format("%016X  ",lineNo*16);
        memcpy(hexLineBuffer,s.c_str(),18);
    }
    else
    {
        wxString s = wxString::Format("%08X  ",lineNo*16);
        memcpy(hexLineBuffer,s.c_str(),10);
    }


    // Set the space between the hex display and the ascii display:
    //                                                                   _
    // 0000000180001000: 00 00 00 00 78 02 35 5E 00 00 00 00 02 00 00 00  ....x.5^........
    int charsToHex = lineData->addressChars + lineData->hexPadChars;
    int charsToSpace = lineData->totalCharsToAscii - lineData->asciiPadChars;
    int charsToAscii = lineData->totalCharsToAscii;
    hexLineBuffer[charsToSpace] = ' ';

    // All remaining characters will be filled in with the following loop:

    unsigned char curChar,upperQuad,lowerQuad;
    int curOffset;

    for ( int j = 0 ; j < charsInLine ; ++j )
    {
        curChar = lineBuffer[j];
        upperQuad = curChar >> 4;
        lowerQuad = curChar & 0x0F;
        curOffset = charsToHex + 3*j;

        hexLineBuffer[curOffset] = HexToAscii(upperQuad);
        hexLineBuffer[curOffset + 1] = HexToAscii(lowerQuad);
        hexLineBuffer[curOffset + 2] = ' ';

        if ( 32 <= curChar && curChar <= 126 )
        {
            hexLineBuffer[charsToAscii+j] = curChar;
        }
        else
        {
            hexLineBuffer[charsToAscii+j] = '.';
        }
    }
}

void MyFrame::DumpBufferToStc(unsigned char* buffer, size_t bufferSize)
{
    LineData lineData(bufferSize);

    int fullLines     = bufferSize/16;
    int extraChars    = bufferSize%16;
    int partialLines  = extraChars?1:0;
    int totalLines    = fullLines + partialLines;
    int totalHexChars = lineData.totalLineCharsWithNewLine *fullLines +
                        lineData.totalCharsToAscii*partialLines + extraChars;

    unsigned char* hexbuffer = new unsigned char[totalHexChars];

    for ( int i = 0 ; i < fullLines ; ++i )
    {
        PrintLine(buffer,hexbuffer,i,16,&lineData);
        hexbuffer[lineData.totalLineCharsWithNewLine*i + lineData.totalLineChars]=0x0A;
    }

    if ( partialLines == 1 )
    {
        // we won't set the space between the end of the hex
        // display area and the start of the ascii display area
        // in the PrintLine function, so do it now.
        memset(hexbuffer + lineData.totalLineCharsWithNewLine*fullLines, 0x20, lineData.totalCharsToAscii);
        PrintLine(buffer, hexbuffer, fullLines, extraChars,&lineData);
    }

    m_stc->ClearAll();
    m_stc->AddTextRaw(reinterpret_cast<char*>(hexbuffer),totalHexChars);

    for ( int i = 0 ; i<totalLines ; ++i )
    {
        m_stc->StartStyling(i*lineData.totalLineCharsWithNewLine);
        m_stc->SetStyling(lineData.addressChars,wxSTC_STYLE_DEFAULT+1);
    }

    delete[] hexbuffer;
}


void MyFrame::OnFile(wxFileDirPickerEvent& event)
{
    wxFile file(event.GetPath(),wxFile::read);
    size_t fileSize =  file.Length();

    unsigned char* buffer = new unsigned char[fileSize];

    file.Read(buffer,fileSize);
    file.Close();

    DumpBufferToStc(buffer,fileSize);

    delete[] buffer;
}


class MyApp : public wxApp
{
    public:
        virtual bool OnInit()
        {
            ::wxInitAllImageHandlers();
            MyFrame* frame = new MyFrame();
            frame->Show();
            return true;
        }
};

wxIMPLEMENT_APP(MyApp);

hexdump.png
hexdump.png (38.39 KiB) Viewed 304 times

ollydbg23
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 202
Joined: Fri Dec 12, 2008 10:31 am

Re: how to display array of hex bytes into control and translate to ascii

Post by ollydbg23 » Tue Sep 29, 2020 10:58 pm

nice work! =D>

seeek
In need of some credit
In need of some credit
Posts: 7
Joined: Tue Sep 29, 2020 1:42 pm

Re: how to display array of hex bytes into control and translate to ascii

Post by seeek » Wed Sep 30, 2020 8:59 am

thank you

I used DumpBufferToStc function .
into my wx dll works very well

but some times these exceptions appear .
I used the dll in run time application and the dll get called by the external application .
do you have any idea how to get the error code reason or handle these exceptions .
they get rises from some where in DumpBufferToStc Function
Attachments
Capture.PNG
Capture.PNG (28.17 KiB) Viewed 224 times
Capture2.PNG
Capture3.PNG

Post Reply