Hex edit control


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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


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.


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.


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


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
Remarks to clear any selection set both s and e to -1


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
Remarks SetBPR stands for SetBytesPerRow


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


Download demo project - 32 KB

Download source - 8 KB


  • read only

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

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

  • Mouse Wheel function

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

    Originally posted by: Eusebiete

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

  • Memory leak, GDI not released

    Posted by Legacy on 10/01/2003 07: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;

    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.


    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 05:46pm

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

  • how to show the horizontal scroll bar?

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

    Originally posted by: benben

    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?


  • A couple of things...

    Posted by Legacy on 06/23/2003 07: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;
    si.nMax = m_length / m_bpr;


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

    I hope this helps!
    -- Raymond Lee

  • Scroll Problem

    Posted by Legacy on 05/30/2003 07: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:
    SetSel(-1, -1);
    OnVScroll(SB_PAGEUP, 0, NULL);

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

    case VK_NEXT:
    SetSel(-1, -1);
    OnVScroll(SB_PAGEDOWN, 0, NULL);

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

  • Coloring elements within the control

    Posted by Legacy on 02/23/2003 08: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?

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

    Posted by Legacy on 12/11/2002 08: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.

    - 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 ...

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

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

    Originally posted by: alwayscy

    Should do this:
    if (m_pData)

    • This problem is discovered 2 years ago.

      Posted by Xiangying on 12/27/2004 03:10pm

      Cheer up!

  • HOWTO: use in visual basic project

    Posted by Legacy on 05/27/2002 07: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.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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