// HexDumpData.cpp : implementation file
//

#include "stdafx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//---------------------------------------------------------------------------
//
// HexDumpData
//
// Description:
//
//    Utitily function for dumping data in a human readable hex format to
//    a CListBox
//
// Parameters:
//
//    ListBoxCtrl - CListBox object to which data is dumped
//    Data - Pointer to contiguous byte array of data
//    Length - Number of bytes Data points to
//
// Return Value:
//
// Remarks:
//
//    This function assumes that Data is a valid pointer to at least
//    Length number of bytes.
//
//---------------------------------------------------------------------------
void HexDumpData(CListBox &ListBoxCtrl, BYTE const * const Data, DWORD const Length)
{
	CString HexString;
	CString CharString;

	for (DWORD i = 0; i < Length; i++)
	{
		BYTE Datum = Data[i];

		CString FormatString;

		FormatString.Format(_T("%.2x "), Datum);
		HexString += FormatString;

		FormatString.Format(_T("%c"), isprint(Datum) ? Datum : _T('.'));
		CharString += FormatString;

		DWORD NextIndex = (i + 1);
		if ((NextIndex % 8 == 0) || (NextIndex == Length))
		{
			CString DumpString;
			DumpString.Format(_T("%-26.26s%s"), (LPCTSTR)HexString, (LPCTSTR)CharString);
			ListBoxCtrl.AddString((LPCTSTR)DumpString);

			HexString.Empty();
			CharString.Empty();
		}
	}
}
