#include #include #include #include int bigedian=0;int uoff=0;int get_aqw(unsigned char c,int aqw) {//0-ascii|1-q|2-wswitch (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;//00000000 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00.................哈哈哈哈哈哈哈哈.................// 954 59 767896 113ln[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]=0xE0 && i=0xE0 && i<len-1) dk=1;elsedk=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=3) {if (strchr(argv[2],'b')) bigedian=1;if (strchr(argv[2],'1')) uoff=1;}printf("=Offset====================HexBytes==========================ANSI============Unicode==========UTF8=======\n");//00000000 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工具处理错误导致正常汉字不能显示或显示为乱码的问题。