hexdump file
1 Create this program step by step with Visualc++.net or VisualC++ 6.0
We open a file to view the content in hex mode
  • 1) Create a MFC project named hexdump

  • In the applicationtype choose SDI "Single Document"

  • In the interfacefeatures choose Maximized

  • Click on button finish


  • 2) Open file hexdumpView.cpp and search:
  • void ChexdumpView::OnDraw(CDC* /*pDC*/)
  • Change this line in:
  • void ChexdumpView::OnDraw(CDC* pDC)
    info: with VisualC++ 6.0 this line needs no change

  • Add code to have the same function as below

  • Compile and see the text
  • void ChexdumpView::OnDraw(CDC* pDC)
    {
    	ChexdumpDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	if (!pDoc)
    		return;
    	// TODO: add draw code for native data here
               pDC->TextOut(100,10,"Hexdump file data");
    	// ==>>  code 01	
    }
    
  • open the file hexdumpDoc.cpp

  • insert the variable iFilesize and cBuffer as below

  • info: they are global variables
    
    // hexdumpDoc.cpp : implementation of the ChexdumpDoc class
    #include "stdafx.h"
    #include "hexdump.h"
    #include "hexdumpDoc.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    unsigned int iFileSize;
    char cBuffer[100000];
    
    
  • now insert the code in function: Serialize

  • only one line you must insert
    void ChexdumpDoc::Serialize(CArchive& ar)
    {
    	if (ar.IsStoring())
    	{
    		// TODO: add storing code here
    	}
    	else
    	{
    		// TODO: add loading code here
    		iFileSize = ar.Read(cBuffer,100000);
    	}
    }
    
    
  • Have you tested the program ? all okay?

  • In the file hexdumpView.cpp declare the same variables from hexdumpDoc.cpp as extern

  • we need a few other variables too.
    info: You can declare all as class variables in the header files, but
    we are going the simplest way and we use global variables
    // hexdumpView.cpp : implementation of the ChexdumpView class
    //
    
    #include "stdafx.h"
    #include "hexdump.h"
    
    #include "hexdumpDoc.h"
    #include "hexdumpView.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    extern unsigned int iFileSize;
    extern char cBuffer[];
    char *ptr1,*ptr2;
    int xx,yy;
    int ii,iLineNumber;
    CString str1,strLine;
    unsigned char chx;
    char cInfoText[20]; 
    
  • Now we extend the code in function OnDraw at the hexdumpView.cpp

  • info: if the cBuffer is empty than iFilesize has the value zero and we return
    otherwise we set the pointer ptr1 at the begin of cBuffer and ptr2 at the end
    	// ==>>  code 01	
    	if(iFileSize == 0) return;
    	ptr1     = cBuffer;
    	ptr2     = ptr1 + iFileSize;	
    	// ==>>  code 02	
    
  • we make the address line for the columns
  • 	// ==>>  code 02
    	
    	strLine.Format("addr    ");
    	ii=0; while(ii < 16) {str1.Format("%02X  ", ii++); strLine+=str1;}
    	xx=10; yy=30;
    	pDC->TextOut(xx,yy,strLine); yy+=25;
    
    	// ==>>  code 03
    	
    
  • We dump 20 lines
  • The variable iLineNumber multiplied with 16 is our first address in the line
  • cInfoText is the string at the end of the line
    	// ==>>  code 03
    	iLineNumber=0;
    	while(iLineNumber < 20)
    	{
    	strLine.Format("%04X   ",iLineNumber*16);
    	ii=0;  while(ii < 16) 
    		{ chx=*ptr1++;  str1.Format("%02X  ", chx); strLine+=str1; 
    			 if(isalnum(chx)) cInfoText[ii]= chx; else cInfoText[ii]= '.';
    		      ii++;
    		}
    
                    str1.Format(" %s", cInfoText); strLine+=str1; 
    		pDC->TextOut(xx,yy,strLine); yy+=18;
    
    		iLineNumber++;
    	}
    
  • control your OnDraw
  • If all is okay start the program and open a file
  • You can open all files you want
  • void ChexdumpView::OnDraw(CDC* pDC)
    {
    	ChexdumpDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	if (!pDoc)
    		return;
    
    	// TODO: add draw code for native data here
    	pDC->TextOut(100,10,"Hexdump file data");
    
    	// ==>>  code 01
    	if(iFileSize == 0) return;
    	ptr1     = cBuffer;
    	ptr2     = ptr1 + iFileSize;	
    
    	// ==>>  code 02
    	strLine.Format("addr    ");
    	ii=0; while(ii < 16) {str1.Format("%02X  ", ii++); strLine+=str1;}
    	xx=10; yy=30;
    	pDC->TextOut(xx,yy,strLine); yy+=25;
    	
    	// ==>>  code 03
    	iLineNumber=0;
    	while(iLineNumber < 20)
    	{
    	strLine.Format("%04X   ",iLineNumber*16);
    	ii=0;  while(ii < 16) 
    		{ chx=*ptr1++;  str1.Format("%02X  ", chx); strLine+=str1; 
    			 if(isalnum(chx)) cInfoText[ii]= chx; else cInfoText[ii]= '.';
    		      ii++;
    		}
    
                    str1.Format(" %s", cInfoText); strLine+=str1; 
    		pDC->TextOut(xx,yy,strLine); yy+=18;
    
    		iLineNumber++;
    	}
    
    }
    
    
  • If you want to have it as below you must insert the following code

  • First you must insert the following line in hexdumpView.cpp OnDraw
  • ChexdumpDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	if (!pDoc)
    		return;
    	   pDC->TextOut(100,10,"Hexdump file data");
    
    	pDC->SelectObject(&m_font);	//only this line
    
    


  • Then insert this lines at the bottom of the page hexdumpView.cpp
  • void ChexdumpView::OnInitialUpdate()
    {
    	CView::OnInitialUpdate();
    
    if (iFontFlag==0)
    {	
    	memset(&logfont,0,sizeof(logfont));
    	logfont.lfHeight=14;
    	logfont.lfWidth=10;
    	CString szFont;
    	szFont="Courier New";
    	lstrcpy(logfont.lfFaceName,szFont);
    	VERIFY(m_font.CreateFontIndirect(&logfont));
    	SetFont(&m_font);
    	iFontFlag=1;
    }
    	
    }
    

  • Then go to hexdumpView.h
  • In the Implementation insert the code as below
  • 	CFont m_font;
    	LOGFONT logfont;
    

  • In Generate massage map functions insert the following lines
  • 	public:
    	virtual void OnInitialUpdate();