(C源代码)一款作者本人原创的高阶命令行HexDump工具

#include <share.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
int bigedian=0;
int uoff=0;
int get_aqw(unsigned char c,int aqw) {//0-ascii|1-q|2-w
    switch (aqw) {
    case 0:if (c> 0x80) aqw=1; else aqw=0; return aqw;
    case 1:if (c>=0x40) aqw=2; else aqw=0; return aqw;
    case 2:if (c> 0x80) aqw=1; else aqw=0; return aqw;
    }
    return aqw;
}
void HexDump(char *buf,int len,int addr) {
    int i,j,k,rw,rg,kk,dk,aqw,aqw0,m;
    char t;
    char binstr[113+1];
    char ln[16+2+1];
    char bb[16];
    WCHAR W[16+2+1];
    BOOL b=TRUE;
    char Wdf[2]={'\xa1','\xf5'};
    unsigned char c0,c1;
//000000  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  .................  哈哈哈哈哈哈哈哈  .................
//       9                                            54   59               7678                96               113
    ln[17]=0;
    kk=0;
    aqw=0;
    aqw0=0;
    for (i=0;i<len;i++) {
        aqw=get_aqw((unsigned char)buf[i],aqw);
        if (0==(i%16)) {
            aqw0=aqw;
            sprintf(binstr,"%08x ",i+addr);
            sprintf(binstr+9," %02x",(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr+54," %02x  ",(unsigned char)buf[i]);
            memcpy(ln,buf+i-15,17);
            for (j=0;j<17;j++) if ((unsigned)ln[j]<' ') ln[j]='.';
            if (aqw!=1) ln[16]=' ';
            if (aqw0==2) ln[0]=' ';
            sprintf(binstr+59,"%s",ln);
            memset(binstr+76,' ',37);
            memcpy(bb,buf+(i-15)+uoff,16);
            if (bigedian) for (m=0;m<15;m+=2) {t=bb[m];bb[m]=bb[m+1];bb[m+1]=t;}
            WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)bb,8,(LPSTR)(binstr+78),16,(LPCSTR)Wdf,&b);
            for (j=78;j<113;j++) if ((unsigned)binstr[j]<' ') binstr[j]='.';
            c0=(unsigned)buf[i];
            c1=(unsigned)buf[i-1];
                 if (c0>=0xE0 && i<len-2) dk=2;
            else if (c1>=0xE0 && i<len-1) dk=1;
            else                          dk=0;
            rw=MultiByteToWideChar(65001,0,(LPCSTR)&buf[i-15+kk],16+dk-kk,(LPWSTR)W,0);
               MultiByteToWideChar(65001,0,(LPCSTR)&buf[i-15+kk],16+dk-kk,(LPWSTR)W,rw);
            rg=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)W,rw,(LPSTR)(binstr+96+kk), 0,(LPCSTR)Wdf,&b);
               WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)W,rw,(LPSTR)(binstr+96+kk),rg,(LPCSTR)Wdf,&b);
            for (j=96+kk+rg;j<113;j++) binstr[j]=' ';
            for (j=96+kk   ;j<113;j++) if ((unsigned)binstr[j]<' ') binstr[j]='.';
            binstr[113]=0;
            printf("%s\n",binstr);
            kk=dk;
        } else if (8==(i%16)) {
            sprintf(binstr+9+(i%16)*3,"-%02x",(unsigned char)buf[i]);
        } else {
            sprintf(binstr+9+(i%16)*3," %02x",(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        k=16-k;
        memset(ln,' ',17);
        memcpy(ln,buf+i-k,k);
        if (aqw0==2) ln[0]=' ';
        for (j=0;j<k;j++) if ((unsigned)ln[j]<' ') ln[j]='.';
        sprintf(binstr,"%s  %s",binstr,ln);
        memset(binstr+76,' ',37);
        memcpy(bb,buf+(i-k)+uoff,k);
        if (bigedian) for (m=0;m<k;m+=2) {t=bb[m];bb[m]=bb[m+1];bb[m+1]=t;}
        WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)&bb,k/2,(LPSTR)(binstr+78),16,(LPCSTR)Wdf,&b);
        for (j=78;j<113;j++) if ((unsigned)binstr[j]<' ') binstr[j]='.';
        rw=MultiByteToWideChar(65001,0,(LPCSTR)&buf[i-k+kk],k-kk,(LPWSTR)W,0);
           MultiByteToWideChar(65001,0,(LPCSTR)&buf[i-k+kk],k-kk,(LPWSTR)W,rw);
        rg=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)W,rw,(LPSTR)(binstr+96+kk), 0,(LPCSTR)Wdf,&b);
           WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)W,rw,(LPSTR)(binstr+96+kk),rg,(LPCSTR)Wdf,&b);
        for (j=96+kk+rg;j<113;j++) binstr[j]=' ';
        for (j=96+kk   ;j<113;j++) if ((unsigned)binstr[j]<' ') binstr[j]='.';
        binstr[113]=0;
        printf("%s\n",binstr);
    }
}
int main(int argc,char **argv) {
    FILE *f;
    int a,r;
    static char buffer[4096+16];

    if (argc<2) {
        fprintf(stderr,"Usage: %s filename.ext [b|1]\n",argv[0]);
        return 2;
    }
    f=_fsopen(argv[1],"rb",_SH_DENYNO);
    if (NULL==f) {
        fprintf(stderr,"Can not open file [%s]!\n",argv[1]);
        return 1;
    }
    if (argc>=3) {
        if (strchr(argv[2],'b')) bigedian=1;
        if (strchr(argv[2],'1')) uoff=1;
    }
    printf("=Offset=  ===================HexBytes====================  ======ANSI=======  =====Unicode====  ======UTF8=======\n");
            //000000  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  .................  哈哈哈哈哈哈哈哈  .................
    a=0;
    memset(buffer+4096,' ',16);
    while (1) {
        r=fread(buffer,1,4096,f);
        HexDump(buffer,r,a);
        a+=r;
        if (r<4096) break;
    }
    fclose(f);
    return 0;
}

Offset,HexBytes,ANSI,Unicode,UTF8字符串同时输出,解决了

◆汉字跨行变乱码

◆Unicode-bigedian

◆Unicode偏移1字节

等一般HexDump工具处理错误导致正常汉字不能显示或显示为乱码的问题。

  • 16
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵4老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值