// hexdumpdata.cpp
//

#include <stdio.h>
#include <string.h>
#include <ctype.h>

//---------------------------------------------------------------------------
//
// HexDumpData
//
// Description:
//
//    Utitily function for dumping data in a human readable hex format to
//    stdout.
//
// Parameters:
//
//    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(unsigned char const * const Data, size_t const Length)
{
	char HexString[25] = {0};
	char CharString[9] = {0};

	for (size_t i = 0; i < Length; i++)
	{
		unsigned char Datum = Data[i];

		char FormatString[4];

		sprintf(FormatString, "%.2x ", Datum);
		strcat(HexString, FormatString);

		sprintf(FormatString, "%c", isprint(Datum) ? Datum : '.');
		strcat(CharString, FormatString);

		size_t NextIndex = (i + 1);
		if ((NextIndex % 8 == 0) || (NextIndex == Length))
		{
			printf("%-26.26s%s\n", HexString, CharString);

			HexString[0] = '\0';
			CharString[0] = '\0';
		}
	}
}
