バッファを16進数と文字列で同時に印刷


この関数は、バッファの内容を印刷するために使用されます.1行に16進数で印刷し、対応する文字列を印刷します.区切る.1行に16バイト印刷します.
 
 
#define PRINT_LINE_WIDTH  16
void PrintChar(FILE*fp, char ch)
{
  if(ch < 16)
      fprintf(fp, "%c", '.');
  else
      fprintf(fp, "%c", ch);  
}
void PrintNullHex(FILE*fp, int count)
{
  int i;

  for(i=0; i<count*3; i++)
  {
      PrintChar(fp, ' ');
  }
}
/******************************************************************************
      : PrintfBuf
      :   buf   ,           
      : fp:         
      : 
       :  
 ******************************************************************************/
void PrintfBuf(FILE *fp, const unsigned char *buf, int len)
{
    int i = 0, j = 0;
    int tail = 0;
    int tail_start = 0;
    const unsigned char *pucBuf = buf;

    if (fp == NULL || len == 0 || NULL == buf)
    {
        return;
    }

    for(i=1; i<=len; i++)
    {
        fprintf(fp, "%02X ", pucBuf[i-1]);
      
        if((i % PRINT_LINE_WIDTH) == 0)
        {
            fprintf(fp, "; ");
            for(j = i - PRINT_LINE_WIDTH; j<i; j++)
            {
                PrintChar(fp,pucBuf[j]);
            }

            fprintf(fp, "\r
"); } } if((len % PRINT_LINE_WIDTH) != 0) tail = PRINT_LINE_WIDTH - (len % PRINT_LINE_WIDTH); if(tail != 0) { PrintNullHex(fp, tail); fprintf(fp, "; "); tail_start = (len / PRINT_LINE_WIDTH) * PRINT_LINE_WIDTH; for(i = tail_start; i<len; i++) { PrintChar(fp, pucBuf[i]); } fprintf(fp, "\r
"); } }