howdays

JIN's Lab

Development

hexdump in C

void hexdump(char *buf, size_t len)
{
  int i , j;
  for(i=0; i<len/16+1; i++)
  {
    printf("%02d | ",i);
    for(j=0; j<16; j++)
    {
      if(i*16+j >= len)
        break;
      printf("%02x ",buf[i*16+j]&0xff);
    }
    printf("| ");
    for(j=0; j<16; j++)
    {
      if(i*16+j >= len)
        break;
      if(isprint(buf[i*16+j]) && buf[i*16+j] != '\n' && buf[i*16+j] != '\t' && buf[i*16+j] != ' ')
      {
        printf("%c",buf[i*16+j]);
      }
      else if(buf[i*16+j] == '\n')
      {
        printf("\\n");
      }
      else if(buf[i*16+j] == '\t' )
      {
        printf("\\t");
      }
      else if(buf[i*16+j] == ' ')
      {
        printf(" ");
      }
      else
      {
        printf(".");
      }
    }
    printf("\n");
  }
}

Leave a Reply