show data

来源:互联网 发布:服务器防ddos攻击软件 编辑:程序博客网 时间:2024/05/04 13:18
#include <stdio.h>
#include <stdlib.h>


#define SHOW_DATA(_buf, _len) func(#_buf, _buf, _len)
void func(const char *name, const char *buffer, const int len)
{
  char tmpbuf[8] = {0};
  char bufout[40] = {0};
  int lenremaining = len;
  const char *bufidx = buffer;
  
  printf("\"name\"'s %d bytes are shown below:\n", len);
  
  while (lenremaining)
  {
    int lentocopy = (lenremaining > 8) ? 8 : lenremaining;
    memset(tmpbuf, 0, 8);
    memset(bufout, 0, 40);
    memcpy(tmpbuf, bufidx, lentocopy);
    lenremaining -= lentocopy;
    
    sprintf(bufout, "%02X-%c\t%02X-%c\t%02X-%c\t%02X-%c\t%02X-%c\t%02X-%c\t%02X-%c\t%02X-%c",
      (unsigned char)tmpbuf[0],(tmpbuf[0]>32&&tmpbuf[0]<127)?tmpbuf[0]:(' '),
      (unsigned char)tmpbuf[1],(tmpbuf[1]>32&&tmpbuf[1]<127)?tmpbuf[1]:(' '),
      (unsigned char)tmpbuf[2],(tmpbuf[2]>32&&tmpbuf[2]<127)?tmpbuf[2]:(' '),
      (unsigned char)tmpbuf[3],(tmpbuf[3]>32&&tmpbuf[3]<127)?tmpbuf[3]:(' '),
      (unsigned char)tmpbuf[4],(tmpbuf[4]>32&&tmpbuf[4]<127)?tmpbuf[4]:(' '),
      (unsigned char)tmpbuf[5],(tmpbuf[5]>32&&tmpbuf[5]<127)?tmpbuf[5]:(' '),
      (unsigned char)tmpbuf[6],(tmpbuf[6]>32&&tmpbuf[6]<127)?tmpbuf[6]:(' '),
      (unsigned char)tmpbuf[7],(tmpbuf[7]>32&&tmpbuf[7]<127)?tmpbuf[7]:(' ')
);

bufidx += 8;

printf("%s\n", bufout);
  }
}


int main(int argc, char *argv[])
{
  char buf[] = {67, -3, 23, 198, 245, 56, 22, -98, 23, 1, -45, 67, 136};
  SHOW_DATA(buf, sizeof(buf));
   
  system("PAUSE");
  return 0;
}