• hexdump


    一个十六进制格式化输出:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 
     6 void hexdump( void *data, unsigned int len )
     7 {
     8     char str[160], octet[10];
     9     int offset, i, k;
    10     unsigned char *buf = (unsigned char *)data;
    11     char dimm[] = "+------------------------------------------------------------------------------+";
    12 
    13     printf("%s
    ", dimm);
    14     printf("| Offset  : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F   0123456789ABCDEF |
    ");
    15     printf("%s
    ", dimm);
    16 
    17     for (offset = 0; offset < len; offset += 16) {
    18         sprintf( str, "| %08x: ", offset );
    19 
    20         for (i = 0; i < 16; i++) {
    21             if ((i + offset) < len)
    22                 sprintf( octet, "%02x ", buf[offset + i] );
    23             else
    24                 strcpy( octet, "   " );
    25 
    26             strcat( str, octet );
    27         }
    28         strcat( str, "  " );
    29         k = strlen( str );
    30 
    31         for (i = 0; i < 16; i++) {
    32             if ((i + offset) < len)
    33                 str[k++] = (0x20 <= (buf[offset + i]) &&  (buf[offset + i]) <= 0x7E) ? buf[offset + i] : '.';
    34             else
    35                 str[k++] = ' ';
    36         }
    37 
    38         str[k] = '';
    39         printf("%s |
    ", str);
    40     }
    41 
    42     printf("%s
    ", dimm);    
    43 }
    44 
    45 int main( void )
    46 {
    47     int i;
    48     unsigned char test_buf[333];
    49     
    50     for( i = 0; i < sizeof(test_buf)/sizeof(test_buf[0]); i++ )
    51     {
    52         test_buf[i] = i % 256;    
    53     }
    54     
    55     hexdump( test_buf, sizeof(test_buf)/sizeof(test_buf[0]) );
    56     
    57     return 0;
    58 }
  • 相关阅读:
    pythonldap 简单试用
    shell 将文件名读入数组
    pytest命令行传入自定义的参数到测试文件中
    Python实现在不同Linux主机之间拷贝文件
    使用minio搭建私有化对象存储服务
    CPU/GPU/NPU
    pytest 内置和自定义marker
    安装SQLite3引发的库问题
    C标准库——程序员等级
    这样还弄不死指针?
  • 原文地址:https://www.cnblogs.com/utank/p/9044451.html
Copyright © 2020-2023  润新知