汉子如何按照16进制打印出来

来源:互联网 发布:如何使用淘宝口令 编辑:程序博客网 时间:2024/05/30 02:23
void PrintRawDataFp(FILE *fp, char *buf, int buflen) {    int ch = 0;    int u = 0;    for (u = 0; u < buflen; u+=16) {        fprintf(fp ," %04X  ", u);        for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {             fprintf(fp, "%02X ", (char)buf[u+ch]&0xff);             if (ch == 7) fprintf(fp, " ");        }        if (ch == 16) fprintf(fp, "  ");        else if (ch < 8) {            int spaces = (16 - ch) * 3 + 2 + 1;            int s = 0;            for ( ; s < spaces; s++) fprintf(fp, " ");        } else if(ch < 16) {            int spaces = (16 - ch) * 3 + 2;            int s = 0;            for ( ; s < spaces; s++) fprintf(fp, " ");        }        for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {             fprintf(fp, "%c", isprint((char)buf[u+ch])&0xff ? (char)buf[u+ch]&0xff : '.');             if (ch == 7)  fprintf(fp, " ");             if (ch == 15) fprintf(fp, "\n");        }    }    if (ch != 16)        fprintf(fp, "\n");}


调用方式:

PrintRawDataFp(stderr,     input,        input_len;


0 0