Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C++
Article

Simple Hex Editor

Rate me:
Please Sign up or sign in to vote.
2.86/5 (12 votes)
27 Jul 20032 min read 117.1K   4.7K   35   14
A simple hex editor

Hex Editor

Introduction

One of the most useful tools for programmers is a good Hex editor. So, I decided to throw one together. There is lots of room for more features, such as find functionality, but it is still a good hex editor. It is a good example of how to implement custom document views.

Description

The applications is SDI, with a view based off of CScrollView. The data is loaded into a CString object. I used CString because of its easy memory management (GetBuffer() and Empty() were all I needed). The view class manually draws the text data twice, in ASCII on the left and in hex on the right. Simple highlighting and cursor control is written into the view class, while data manipulation is handled by the data interface. It was really more for my own experience, but I decided that it might be a handy example for fellow programmers on a similar path.

Points of interest

The scrolling may or may not be implemented the way it was intended. I couldn't find very good documentation on how to get it working perfectly, so I set the origins to the top left and simply calculated the visible lines myself. There may be a better way to do that (if there is, tell me), but the end result is that I only draw what I need, making the rendering fast enough to be usable. I also managed to avoid erasing the screen for everything except scrolling.

Known bugs

There are probably quite a few. Meh.

Updates

July 24, 2003 - Fixed the package, fixed drawing to use a memory dc making scrolling smooth, attempted to fix mousewheel scrolling (unable to confirm, no mouse to test with), used window color instead of plain white for background, disabled printing, fixed signed/unsigned warnings.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionNew feature Pin
WillianBR5-Jan-12 6:06
WillianBR5-Jan-12 6:06 
GeneralQuick 'n' dirty way Pin
andwan07-Jun-09 11:45
andwan07-Jun-09 11:45 
GeneralImportant Question Pin
Fabian Ganter7-Aug-08 3:12
Fabian Ganter7-Aug-08 3:12 
Hello Paul Frazee,
my name is Fabian Ganter and I´m from Germany.
First thank´s for your Simple Hex Editor.

I have a important Problem. I had token your Hex-Editor and modify it a little bit.
I would the output change and make an adress line(gap) to it. So that was no problem and it worked. But if I scroll with the mouse the scrollbar up and down, the new adress line redraw it always on top of the window and the hexdataout and the chardatatout are switching. I think it is something to change in the code at "void CHexEditorView::OnVScroll", or?
So I hope you know what I mean and perhaps you can help me.

Thank you very much!

Best regarts Fabian Ganter

Sourcecode from HexEditorView:
// calculate rects
CRect rcCharData, rcHexData, rcNumData;
// hex
rcHexData.left = rcRectClient.left + 120;
rcHexData.top = rcRectClient.top ;
rcHexData.right = rcRectClient.right / 2 + 60;
rcHexData.bottom = rcRectClient.bottom - 10;
if( rcHexData.Width() % 20 != 0 )
rcHexData.right -= rcHexData.Width() % 20;
nMaxX = rcHexData.Width() / 20;
// data
rcCharData.left = rcRectClient.right / 2 + 70;
rcCharData.top = rcRectClient.top ;
rcCharData.right = rcRectClient.right;
rcCharData.bottom = rcRectClient.bottom - 10;
if( rcCharData.Width() % 20 != 0 )
rcCharData.right -= rcCharData.Width() % 20;
//adresse
rcNumData.left = rcRectClient.left+40;
rcNumData.top = rcRectClient.top;
rcNumData.right = rcRectClient.right / 2 + 185;
rcNumData.bottom = rcRectClient.bottom - 10;
if( rcNumData.Width() % 20 !=0)
rcNumData.right -= rcNumData.Width() % 20;

// handle the mouse input
if( m_ptMouse.x != -1 )
{ BOOL fValid = TRUE;
if( rcCharData.PtInRect( m_ptMouse ) )
{
m_ptMouse.x -= rcCharData.left;
m_ptMouse.y -= rcCharData.top;
} else if( rcHexData.PtInRect( m_ptMouse ) )
{
m_ptMouse.x -= rcHexData.left;
m_ptMouse.y -= rcHexData.top;
} else
fValid = FALSE;

if( fValid )
{
int nX = ( m_ptMouse.x - ( m_ptMouse.x % 20 ) ) / 20;
int nY = ( m_ptMouse.y - ( m_ptMouse.y % 20 ) ) / 20;
int nSelAdd = nX + ( nY * m_nLineLength ) + m_nVisStart;
if( !m_fDraggingMouse )
m_nCurSel = nSelAdd;
m_nCurSelEnd = nSelAdd;
}
}

CRect rcOut;
unsigned int nAddX;
//write out the adress buffer
CString szOut;
rcOut = rcNumData;
nAddX= 0;
const int v=100;
int z=0;
int Daten[v];
for(int s=0; s <=v;s++)
{
Daten[s]=z;
z += m_nLineLength-1;
}
for(int j=0; j < v; j++)
{
if( j != 0)
{

rcOut.top += 20;
}

szOut.Format( "%08xh:", Daten[j]);
szOut.MakeUpper();
pMemDC->DrawText( szOut , &rcOut, DT_LEFT | DT_NOCLIP );
}

// write out the data buffer
rcOut = rcCharData;
nAddX = 0;
LPTSTR pData = pDoc->m_szDataBuffer.GetBuffer( pDoc->m_nBufferLength );
for( i=m_nVisStart; i < m_nVisEnd; i++ )
{
//TRACE("m_nVisStart: %d m_nVisEnd: %d\n",m_nVisStart,m_nVisEnd);
// if it is over, increment our y
if( ( i - m_nVisStart ) != 0 && ( i - m_nVisStart ) % nMaxX == 0 )
{
nAddX = 0;
rcOut.top += 20;
}
// make the addition
rcOut.left = rcCharData.left + nAddX;
// fill behind current selection
if( ( i >= m_nCurSel && i <= m_nCurSelEnd ) || ( i >= m_nCurSelEnd && i <= m_nCurSel ) )
{
pMemDC->SetBkColor( RGB( 220, 220, 220 ) );
} else
{
pMemDC->SetBkColor( GetSysColor( COLOR_WINDOW ) );
}
// draw the text
if( pData[i] )
pMemDC->DrawText( pData[i], &rcOut, DT_LEFT | DT_NOCLIP );
// add
nAddX += 17;
}

// write out the data buffer in hex
rcOut = rcHexData;
nAddX = 0;

pData = pDoc->m_szDataBuffer.GetBuffer( pDoc->m_nBufferLength );
for( i=m_nVisStart; i < m_nVisEnd - 1; i++ )
{
// if it is over, increment our y
if( ( i - m_nVisStart ) != 0 && ( ( i - m_nVisStart ) % nMaxX ) == 0 )
{
nAddX = 0;
rcOut.top += 20;
}
// make the addition
rcOut.left = rcHexData.left + nAddX;
// fill behind current selection
if( ( i >= m_nCurSel && i <= m_nCurSelEnd ) || ( i >= m_nCurSelEnd && i <= m_nCurSel ) )
{
pMemDC->SetBkColor( RGB( 220, 220, 220 ) );
} else
{
pMemDC->SetBkColor( GetSysColor( COLOR_WINDOW ) );
}
// draw the text
szOut.Format( "%02x", (unsigned char)pData[i] );
szOut.MakeUpper();
pMemDC->DrawText( szOut, &rcOut, DT_LEFT | DT_NOCLIP );
// add
nAddX += 17;
}

// reset the old object
if( pOldObj )
pMemDC->SelectObject( pOldObj );

// copy to the real device context
pDC->BitBlt( 0, 0, rcRectClient.right, rcRectClient.bottom, pMemDC, 0, 0, SRCCOPY );

// restore dc
if( pOldBitmap )
pMemDC->SelectObject( pOldBitmap );
bMemBMP.DeleteObject();
pDC->RestoreDC( nSavedDC );
}

void CHexEditorView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
UpdateScrollbars();
}

/////////////////////////////////////////////////////////////////////////////
// CHexEditorView printing

BOOL CHexEditorView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
//return DoPreparePrinting(pInfo);
// WILL BE IN NEXT VERSION
MessageBox( "Printing Not Yet Implemented" );
return FALSE;
}

void CHexEditorView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CHexEditorView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CHexEditorView diagnostics

#ifdef _DEBUG
void CHexEditorView::AssertValid() const
{
CScrollView::AssertValid();
}

void CHexEditorView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}

CHexEditorDoc* CHexEditorView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHexEditorDoc)));
return (CHexEditorDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHexEditorView message handlers

void CHexEditorView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
BOOL fInvalidate = FALSE;
BOOL fErase = FALSE;
if( nChar == VK_LEFT )
{
if( m_nCurSel > 0 ) m_nCurSelEnd = --m_nCurSel;
fInvalidate = TRUE;
}
if( nChar == VK_RIGHT )
{
m_nCurSelEnd = ++m_nCurSel;
if( m_nCurSel >= GetDocument()->m_nBufferLength )
m_nCurSel = GetDocument()->m_nBufferLength - 1;
fInvalidate = TRUE;
}
if( nChar == VK_UP )
{
m_nCurSel -= m_nLineLength;
if( m_nCurSel < 0 ) m_nCurSel = 0;
m_nCurSelEnd = m_nCurSel;
fInvalidate = TRUE;
}
if( nChar == VK_DOWN )
{
m_nCurSel += m_nLineLength;
if( m_nCurSel >= GetDocument()->m_nBufferLength )
m_nCurSel = GetDocument()->m_nBufferLength - 1;
m_nCurSelEnd = m_nCurSel;
fInvalidate = TRUE;
}
if( nChar == VK_DELETE || nChar == VK_BACK )
{
// error checking
if( ( nChar == VK_BACK && m_nCurSel <= 0 ) || m_nCurSel >= GetDocument()->m_nBufferLength - 1 )
return;
// delete the data
unsigned int nStart, nEnd;
GetSel( nStart, nEnd );
GetDocument()->DeleteData( nStart, nEnd - nStart + 1 );
// move the cursor
if( nChar == VK_BACK ) m_nCurSel = m_nCurSelEnd = ( nStart - 1 );
else m_nCurSelEnd = m_nCurSel = nStart;
// update scroll bars
UpdateScrollbars();
fErase = fInvalidate = TRUE;
}

if( fInvalidate )
{
MakeCursorVisible();
Invalidate( fErase );
return;
}

CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL CHexEditorView::OnEraseBkgnd(CDC* pDC)
{
return FALSE;//CScrollView::OnEraseBkgnd(pDC);
}

void CHexEditorView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// windows doesnt natively use 32 bit data to track the pos
// so if we can, we need to get it
TRACE("VSCROLL event \n");
if( nSBCode == SB_THUMBTRACK || nSBCode == SB_THUMBPOSITION )
{
HWND hWndScroll = pScrollBar ? pScrollBar->m_hWnd : m_hWnd;
SCROLLINFO siInfo;
siInfo.cbSize = sizeof( SCROLLINFO );
siInfo.fMask = SIF_TRACKPOS;
::GetScrollInfo( hWndScroll, SB_VERT, &siInfo );
nPos = siInfo.nTrackPos;
rectpos=nPos;
TRACE("nPos= %d \n",nPos);

}

CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
Invalidate( FALSE );
}

void CHexEditorView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
Invalidate( FALSE );
}

void CHexEditorView::OnLButtonDown(UINT nFlags, CPoint point)
{ TRACE("OnLButtonDown event \n");
m_ptMouse = point;
CScrollView::OnLButtonDown(nFlags, point);
Invalidate( FALSE );
}

void CHexEditorView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_fDraggingMouse = FALSE;
m_ptMouse.x = m_ptMouse.y = -1;
Invalidate( FALSE );
CScrollView::OnLButtonUp(nFlags, point);
}

void CHexEditorView::OnMouseMove(UINT nFlags, CPoint point)
{ //TRACE("OnMouseMove event \n");
if( m_ptMouse.x != -1 )
{
m_fDraggingMouse = TRUE;
m_ptMouse = point;
Invalidate( FALSE );
}

CScrollView::OnMouseMove(nFlags, point);
}


void CHexEditorView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
if( cx || cy )
UpdateScrollbars();
}

void CHexEditorView::OnRButtonUp(UINT nFlags, CPoint point)
{
// load the menu
CMenu cMenu;
cMenu.LoadMenu( IDR_CONTEXT );
CMenu* pPopup = cMenu.GetSubMenu( 0 );

// convert point
ClientToScreen( &point );

// track popup menu
DWORD dwSelection = pPopup->TrackPopupMenu( (TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD ),
point.x, point.y, this );
// destroy it
pPopup->DestroyMenu();

// act on selection
if( dwSelection == ID_CONTEXT_INSERTBYTES )
GetDocument()->InsertBytes(); // use document's functionality
if( dwSelection == ID_CONTEXT_EDITINTEGER || dwSelection == ID_CONTEXT_EDITHEX )
{
// get the selection
unsigned int nStart, nEnd;
GetSel( nStart, nEnd );
// make sure they havent selected more than 4 bytes
if( nEnd + 1 - nStart > 4 )
{
MessageBox( "Too many bytes selected - max 4", "Error" );
return;
}
// edit the data
GetDocument()->EditData( nStart, nEnd, ( dwSelection == ID_CONTEXT_EDITHEX ) );
Invalidate( TRUE );
}
if( dwSelection == ID_CONTEXT_EDITSTRING )
{
// get the selection
unsigned int nStart, nEnd;
GetSel( nStart, nEnd );
// edit the data
GetDocument()->EditString( nStart, nEnd );
Invalidate( TRUE );
}

CScrollView::OnRButtonUp(nFlags, point);
}

LONG CHexEditorView :: OnWheel( UINT nFlags, LONG pValue )
{

if( nFlags & 0x0100 ) // rolled up
{//TRACE("OnWheel event \n");
OnVScroll( SB_LINEUP, 0, NULL );
}
else // rolled down
{
OnVScroll( SB_LINEDOWN, 0, NULL );
}
return 0;
}
GeneralHex Edit 2.5F Pin
Andrew Phillips7-Apr-08 18:22
Andrew Phillips7-Apr-08 18:22 
GeneralFree hex editor with MFC source Pin
Andrew Phillips1-Mar-05 12:02
Andrew Phillips1-Mar-05 12:02 
GeneralRe: Free hex editor with MFC source Pin
PaulFrazee2-Mar-05 11:25
PaulFrazee2-Mar-05 11:25 
GeneralThis is not an editor... it is a viewer! Pin
Spiceworm22-Feb-05 10:55
Spiceworm22-Feb-05 10:55 
GeneralRe: This is not an editor... it is a viewer! Pin
PaulFrazee22-Feb-05 17:15
PaulFrazee22-Feb-05 17:15 
GeneralShould be more function. Pin
ICE_WIZARD22-Jul-03 6:18
ICE_WIZARD22-Jul-03 6:18 
GeneralQuick and Dirty fixes Pin
McGarrah21-Jul-03 15:55
McGarrah21-Jul-03 15:55 
GeneralRe: Quick and Dirty fixes Pin
PaulFrazee22-Jul-03 11:15
PaulFrazee22-Jul-03 11:15 
GeneralMissing files &amp; bugs Pin
billgatest19-Jul-03 1:27
billgatest19-Jul-03 1:27 
GeneralRe: Missing files &amp; bugs Pin
PaulFrazee19-Jul-03 8:42
PaulFrazee19-Jul-03 8:42 
GeneralScrolling doesn't work Pin
Dominik Reichl18-Jul-03 6:01
Dominik Reichl18-Jul-03 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.