I wonder if some needs such utility.
This function allows to present (print) binary data stream in hexadecimal format.
PHP Code:
void OutputInHexFormat(char *pBuf, int iLen)
{
#define OUTPUTWIDTH 50
static char outputBuf[300];
int i, intCtr, iLenStr;
char tmp[20];
char *pCursor;
memset(outputBuf, 0, sizeof(outputBuf));
pCursor = outputBuf;
intCtr = 0;
for (i = 0; i < iLen; i++)
{
sprintf(tmp, "%02x ", pBuf[i] & (int)0xFF);
iLenStr = strlen(tmp);
intCtr += iLenStr;
strcat(pCursor, tmp); // append new data
pCursor += iLenStr; // advance destination pointer
// check the output width:
if ( ( pCursor - outputBuf ) > OUTPUTWIDTH-1 )
{
printf(" %s\n", outputBuf );
pCursor = outputBuf; // reset to the begin
// prepare the storage:
memset(outputBuf, 0, intCtr*sizeof(char) );
}
}
printf(" %s\n", outputBuf );
}
To make it faster use table of strings {"00", "01", "02", ..., "ff "} instead of constructing string each time:
PHP Code:
sprintf(tmp, "%02x ", pBuf[i] & (int)0xFF);
also replace this call
PHP Code:
iLenStr = strlen(tmp);
to