printf函数十六进制格式化输出并且2字节对齐

来源:互联网 发布:听音记谱软件 编辑:程序博客网 时间:2024/06/01 13:03

printf函数十六进制格式化输出并且2字节对齐


"%02X",是以0补齐2位数,如果超过2位就显示实际的数,字母数值大写,如果换为x,字母数值就小写。

实例:

#include<stdio.h>
void hexdump(const unsigned char *buf, const int num)
{
    int i;
    for(i = 0; i < num; i++)
    {
        printf("%02X ", buf[i]);
        if ((i+1)%8 == 0)
            printf("\n");
    }
    printf("\n");
    return;
}

void main()
{
    unsigned char buf[23] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,254,255};
    hexdump(buf, 23);
    return;
}


执行结果:

00 01 02 03 04 05 06 07
08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 FE FF

原创粉丝点击