PROWAREtech

articles » current » windows » microsoft-foundation-classes » hex-dump

Windows MFC: HexDump Example Application

A hexadecimal display application built with Microsoft Foundation Classes.

This MFC application opens a file to display its contents in a hexadecimal format. It is a simple application to learn the Microsoft Foundation Classes (MFC) Document/View Architecture. This code comes from Programming Windows with MFC Second Edition by Jeff Prosise. Compare it to the .NET HexDump. The meat of the program is listed below but the whole source can be found here: MFCHEXDUMP.zip

void CHexDumpView::FormatLine(CHexDumpDoc *pDoc, UINT nLine, CString &string)
{
	UINT i;

	// get 16 bytes and format them
	BYTE b[17];
	::FillMemory(b, 16, 32);
	UINT nCount = pDoc->GetBytes(nLine * 16, 16, b);

	string.Format(_T("%08X	 %0.2X %0.2X %0.2X %0.2X %0.2X %0.2X %0.2X %0.2X - %0.2X %0.2X %0.2X %0.2X %0.2X %0.2X %0.2X %0.2X	 "),
		nLine * 16, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]);

	// replace non-printable chars with periods
	for(i = 0; i < nCount; i++)
	{
		if(!::IsCharAlphaNumeric(b[i]))
			b[i] = 0x2E;
	}

	// if less than 16 bytes were retrieved then erase to the end of line
	b[nCount] = 0;
	string += b;

	if(nCount < 16)
	{
		UINT pos1 = 59, pos2 = 60;
		UINT j = 16 - nCount;

		for(i = 0; i < j; i++)
		{
			string.SetAt(pos1, _T(' '));
			string.SetAt(pos2, _T(' '));
			pos1 -= 3;
			pos2 -= 3;
			if(pos1 == 35)
			{
				string.SetAt(35, _T(' '));
				string.SetAt(36, _T(' '));
				pos1 = 33;
				pos2 = 34;
			}
		}
	}

}
MFC HexDump Example Application

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE