Hex edit control

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

The HexEdit control is a plain hex editor wrapped in a CEdit derived class called CHexEdit. Where ever you use a CEdit class you can replace it with the CHexEdit class. CHexEdit supports the following features:

  1. Addressdisplay can be short or long or turned off.
  2. Hexdisplay can have any width between 1 and 256 bytes or turned off
  3. Asciidisplay can have any width between 1 and 256 bytes or turned off
  4. Editing in hex or ascii mode
  5. cut/copy/paste support (text and/or binary format, binary compatible with DevStudio)

Undo is in the context menu but not implemented right now. Horizontal scrolling is also not supported right now.

Public Methods

GetData

int GetData(LPBYTE p, int len);
Use GetData to retrieve the modified data from the control.
Parameter LPBYTE p pointer to the databuffer
  int len length of the databuffer
Return value number of bytes copied  
Remarks the GetData Method copies the data to the buffer provided with the parameter p.

SetData

void SetData(LPBYTE p, int len);
Use SetData to set the content that the control displays for editing
Parameter LPBYTE p pointer to the databuffer
  int len length of the databuffer
Return value none  
Remarks the SetData Method copies the data from the buffer provided with the parameter p into a internal working copy.

GetSel

CSize GetSel(void);
Use GetSel to retrieve the range of bytes the user has selected.
Parameter none  
Return value
CSize
where cx contains the start and cy contains the end of the selection.
Remarks if nothing is selected both cx and cy are -1

SetSel

void SetSel(int s, int e);
Use SetSel to set the selection.
Parameter int s start of selection
  int e end of selection
Return value
none
 
Remarks to clear any selection set both s and e to -1

SetBPR

void SetBPR(int bpr);
Use SetBPR to set the number of bytes per row that will be displayed.
Parameter int bpr number of bytes to display per row
Return value
none
 
Remarks SetBPR stands for SetBytesPerRow

SetOptions

void SetOptions(BOOL a, BOOL h, BOOL c, BOOL w);
Used to set various options.
Parameter BOOL a addressdisplay on/off
  BOOL h hexdisplay on/off
  BOOL c asciidisplay on/off
  BOOL w address is short (FALSE) or long (TRUE)
Return value
none
 
Remarks  

 

Download demo project - 32 KB

Download source - 8 KB



Comments

  • read only

    Posted by olga145 on 05/30/2005 04:08am

    How can I make this edit "read only"? Thank you.

    Reply
  • Mouse Wheel function

    Posted by Legacy on 01/05/2004 12:00am

    Originally posted by: Eusebiete

    BOOL CHexEditEE::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt){
    
    if(!m_pData)
    return CEdit::OnMouseWheel(nFlags, zDelta, pt);
    int oa = m_topindex;
    if(zDelta>0){ //es pa arriba
    if(m_topindex > 0){
    m_topindex -= (m_bpr);
    RedrawWindow();
    }
    }
    if(zDelta<0){ //es pa abajo
    if(m_topindex < m_length - m_lpp*m_bpr){
    m_topindex += (m_bpr);
    RedrawWindow();
    }
    }
    ::SetScrollPos(this->m_hWnd, SB_VERT, m_topindex / (m_bpr), TRUE);
    if(!m_bNoAddressChange) m_currentAddress += (m_topindex - oa);
    RepositionCaret(m_currentAddress);
    return CEdit::OnMouseWheel(nFlags, zDelta, pt);
    }

    Reply
  • Memory leak, GDI not released

    Posted by Legacy on 10/01/2003 12:00am

    Originally posted by: MemoryLove

    The memory leak is not due to m_pData but to GDI ressources.
    
    

    In method :

    void CHexEdit::OnPaint()
    {
    CPaintDC pdc(this); // device context for painting
    ...
    }

    you have the code lines :

    CDC dc;
    dc.CreateCompatibleDC(CDC::FromHandle(pdc.m_ps.hdc));

    but this lines allocates ressources and even if dc is a local variable and will be destructed at the end of method, you have to explicitly call the free method.

    For fixes the buf, add this code line as the last code line of 'void CHexEdit::OnPaint()' method.

    dc.DeleteDC();

    Done it ? Ok check your memory and you will seee that you have no more memory leak ;-)

    • How to locate address

      Posted by random263 on 06/21/2004 10:46am

      I want to locate a given address, and scroll to view it's content, what should I do?

      Reply
    Reply
  • how to show the horizontal scroll bar?

    Posted by Legacy on 07/16/2003 12:00am

    Originally posted by: benben

    Hi:
    The hexedit is great, but I find when I change number of the characters per line(horizontal scroll bar is necessary now), no horizontal scroll bar will show.How to fix it?

    Thanks
    benben

    Reply
  • A couple of things...

    Posted by Legacy on 06/23/2003 12:00am

    Originally posted by: Raymond Lee

    First off, great control! It's making my life easier at this very moment. :)

    A couple of things. First, I use this control in a splitter window. The problem is if I have a lot of data in the edit control and I shorten the pane, no vertical scrollbar appears. To remedy this, I wrote:

    void CHexEdit::OnSize(UINT nType, int cx, int cy)
    {
    //
    // Cause the scrollbars to appear if necessary.
    //
    m_bUpdate = TRUE;

    CEdit::OnSize(nType, cx, cy);
    }

    The second thing is I noticed the vertical scrollbar wasn't being calculated correctly. If one line of text is off the screen you cannot scroll down to it. To fix it, modify the two lines shown here in void CHexEdit::UpdateScrollbars()

    si.nMax = (m_length / m_bpr) - 1;
    BECOMES
    si.nMax = m_length / m_bpr;

    AND...

    if(si.nMax > (int)si.nPage)
    BECOMES
    if(si.nMax > (int)si.nPage - 1)


    I hope this helps!
    -- Raymond Lee

    Reply
  • Scroll Problem

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: carmelo

    I'm sorry i don�t speek English.
    I think there are some little bugs in this class.
    The problem is the reference Scroll in the event OnKeyDown().

    case VK_PRIOR:
    .....
    else
    SetSel(-1, -1);
    OnVScroll(SB_PAGEUP, 0, NULL);
    Move(0,0);
    break;

    -Replace for this:
    ......
    else
    SetSel(-1, -1);
    m_bNoAddressChange = FALSE;
    OnVScroll(SB_PAGEUP, 0, NULL); break;

    case VK_NEXT:
    .....
    else
    SetSel(-1, -1);
    OnVScroll(SB_PAGEDOWN, 0, NULL);
    Move(0,0);
    break;

    -Replace for this:
    ......
    else
    SetSel(-1, -1);
    m_bNoAddressChange = FALSE;
    OnVScroll(SB_PAGEDOWN, 0, NULL); break;

    Reply
  • Coloring elements within the control

    Posted by Legacy on 02/23/2003 12:00am

    Originally posted by: Martijn

    I'd like to highlight certain ranges (or single characters) within the total range.. Is there any way to set the background (or foreground) color of those ranges?

    Reply
  • BUG - missing last character when using copy on ASCII data col

    Posted by Legacy on 12/11/2002 12:00am

    Originally posted by: XM

    Thanks for this control, it helps me.
    
    

    Everytime I tried to copy (Ctrl+C) data displayed in the
    ascii-data representing column I got one char less - the
    last one.

    Solution:
    - in the member function OnEditCopy() increase the size
    of the membuffer (1 byte more), so ...

    in the case that we have "m_currentMode==EDIT_ASCII"

    HGLOBAL hMema = ::GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT, m_selEnd-m_selStart+1); // +1 !


    This is due to the nature of text/strings - they have to be
    null-terminated, and there was not a room for it ...


    Reply
  • No one found? He didn't free m_pData always!

    Posted by Legacy on 07/11/2002 12:00am

    Originally posted by: alwayscy

    Should do this:
    CHexEdit::~CHexEdit()
    {
    if (m_pData)
    free(m_pData);
    }

    • This problem is discovered 2 years ago.

      Posted by Xiangying on 12/27/2004 07:10am

      Cheer up!

      Reply
    Reply
  • HOWTO: use in visual basic project

    Posted by Legacy on 05/27/2002 12:00am

    Originally posted by: Ashwi Ambekar

    I need to use the HexEdit in my visual basic code. I registerd the OCX, now when I call SetData, I get error while compiling ,
    "Function or interface marked as restricted, or function uses automation type not supported in Visual basic.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When moving workloads to Amazon Web Services (AWS), a comprehensive data protection strategy is key. Check out these 5 important data protection steps designed to make your move to AWS successful.

  • Whether you're wondering if the title of "developer" should have your name next to it or you've just started on the path to software success, consider this guide your personal README.md file. (And if that reference didn't make sense to you, read on. It will soon, grasshopper.) This software developer resource guide explores just how in-demand developers are and the growing gap of qualified individuals needed, discusses the many benefits and perks of being a developer, and enlightens you on the myriad of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date