Chinaunix首页 | 论坛 | 博客
  • 博客访问: 398968
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-03 18:24
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(20)

2017年(14)

2016年(10)

2015年(30)

分类: LINUX

2017-04-23 19:02:34


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. void Print_hex_data(const char *pComment, const char *pBuff, int buf_size)
  4. {
  5. #define _LINE_BYTES_    16
  6.     int line_index = 0;
  7.     int byte_index = 0;
  8.     int total_line = (buf_size + _LINE_BYTES_ - 1) / _LINE_BYTES_;
  9.     int line_bytes = 0;

  10.     if(pComment != NULL)
  11.     {
  12.         printf("%s\n", pComment);
  13.     }
  14.     printf("#############################################(total size:%d)\n", buf_size);

  15.     /**/
  16.     for(line_index = 0; line_index < total_line; line_index ++)
  17.     {
  18.         printf("%p:", &pBuff[line_index * _LINE_BYTES_]);
  19.         line_bytes = buf_size - (line_index * _LINE_BYTES_);
  20.         if(line_bytes > _LINE_BYTES_)
  21.         {
  22.             line_bytes = _LINE_BYTES_;
  23.         }

  24.         /**/
  25.         for(byte_index = 0; byte_index < line_bytes; byte_index ++)
  26.         {
  27.             printf("%02x ", pBuff[line_index * _LINE_BYTES_ + byte_index]);
  28.         }
  29.         for(; byte_index < _LINE_BYTES_; byte_index ++)
  30.         {
  31.             printf(" ");
  32.         }

  33.         /**/
  34.         printf(" ;");
  35.         for(byte_index = 0; byte_index < line_bytes; byte_index ++)
  36.         {
  37.             if((pBuff[line_index * _LINE_BYTES_ + byte_index] >= 32) &&
  38.                     (pBuff[line_index * _LINE_BYTES_ + byte_index] <= 126))
  39.             {
  40.                 printf("%c", pBuff[line_index * _LINE_BYTES_ + byte_index]);
  41.             }
  42.             else
  43.             {
  44.                 printf(".");
  45.             }
  46.         }
  47.         printf("\n");
  48.     }
  49.     printf("#############################################\n");
  50. }

  51. int main(int argc, char **argv)
  52. {
  53.     char buf[500] = "adsahdsafdsfdsgfdhgfjhgjhgjghjhgjfghvcbcvbcbcbdfd";
  54.     
  55.     Print_hex_data("send", buf, strlen(buf));
  56.     return 0;
  57. }
运行结果:
阅读(687) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~