在linux下通过hexdump生成一个十六进制的文本保存文件,解析此文件转变成正常源代码文件。

举例说明:

此十六进制保存的文件为此源代码hexdump生成的:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
dtob(int a);
int main(int argc,char **argv)
{
        FILE *fp;
        int a;
        if((fp=fopen("1.c","r"))==NULL)
        {
                printf("open error
");
        }
        while((a=getc(fp))!=-1)
        {
                dtob(a);
        }
        printf("
");
        return 0;
}
dtob(int a)
{
        int i=0;
        int *p;
        p=(int *)malloc(8*sizeof(int));
        while(i<8)
        {
                if(a/2 || a==1)
                {
                        *(p+i)=a%2;
                }
                else
                        *(p+i)=0;
                a=a/2;
                i++;
        }
        for(i=7;i>=0;i--)
                printf("%d",*(p+i));
}

此源代码的hexdump之后的文件为:

0000000 6923 636e 756c 6564 733c 6474 6f69 682e
0000010 0a3e 6923 636e 756c 6564 733c 7274 6e69
0000020 2e67 3e68 230a 6e69 6c63 6475 3c65 7473
0000030 6c64 6269 682e 0a3e 6923 636e 756c 6564
0000040 6d3c 6c61 6f6c 2e63 3e68 640a 6f74 2862
0000050 6e69 2074 2961 0a3b 6e69 2074 616d 6e69
0000060 6928 746e 6120 6772 2c63 6863 7261 2a20
0000070 612a 6772 2976 7b0a 090a 4946 454c 2a20
0000080 7066 0a3b 6909 746e 6120 0a3b 6909 2866
0000090 6628 3d70 6f66 6570 286e 3122 632e 2c22
00000a0 7222 2922 3d29 4e3d 4c55 294c 090a 0a7b
00000b0 0909 7270 6e69 6674 2228 706f 6e65 6520
00000c0 7272 726f 6e5c 2922 0a3b 7d09 090a 6877
00000d0 6c69 2865 6128 673d 7465 2863 7066 2929
00000e0 3d21 312d 0a29 7b09 090a 6409 6f74 2862
00000f0 2961 0a3b 7d09 090a 7270 6e69 6674 2228
0000100 6e5c 2922 0a3b 7209 7465 7275 206e 3b30
0000110 7d0a 640a 6f74 2862 6e69 2074 2961 7b0a
0000120 090a 6e69 2074 3d69 3b30 090a 6e69 2074
0000130 702a 0a3b 7009 283d 6e69 2074 292a 616d
0000140 6c6c 636f 3828 732a 7a69 6f65 2866 6e69
0000150 2974 3b29 090a 6877 6c69 2865 3c69 2938
0000160 090a 0a7b 0909 6669 6128 322f 7c20 207c
0000170 3d61 313d 0a29 0909 0a7b 0909 2a09 7028
0000180 692b 3d29 2561 3b32 090a 7d09 090a 6509
0000190 736c 0a65 0909 2a09 7028 692b 3d29 3b30
00001a0 090a 6109 613d 322f 0a3b 0909 2b69 3b2b
00001b0 090a 0a7d 6609 726f 6928 373d 693b 3d3e
00001c0 3b30 2d69 292d 090a 7009 6972 746e 2866
00001d0 2522 2264 2a2c 7028 692b 2929 0a3b 0a7d
00001e0

如何把此十六进制代码重新解析返回成源代码形成:

编码如#include<stdio.h>

#include<string.h>
#include<stdlib.h>
disp(char *p);
int main(int argc,char **argv)
{
        FILE *fp;
        char buf[100],*p;
        p=(char *)malloc(sizeof(buf));
        if((fp=fopen("hex","r"))==NULL)
        {
                printf("open error!
");
                exit(0);
        }
        memset(buf,0,sizeof(buf));
        while(fgets(buf,sizeof(buf),fp)!=NULL)
        {
                if((p=strstr(buf," "))!=NULL)
                        p++;
                disp(p);
                memset(buf,0,sizeof(buf));
        }
        return 0;
}
disp(char *p)
{
        char a[2],b[2];
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        while(p!=NULL)
        {
                sscanf(p,"%2x%2x",b,a);
                printf("%s%s",a,b);
                if((p=strstr(p," "))!=NULL)
                {
                        p++;
              if(strncmp(p," ",1)==0)  
              exit(0);
                }
        }
}

 在IBM的AIX下:

00000(00000) 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31  <?xml version="1
00016(00010) 2e 30 22 20 20 65 6e 63 6f 64 69 6e 67 3d 22 47  .0"  encoding="G
00032(00020) 42 4b 22 20 3f 3e 0a 3c 4d 73 67 3e 0a 09 3c 48  BK" ?>*<Msg>**<H
00048(00030) 65 61 64 3e 0a 09 09 3c 56 65 72 3e 31 2e 30 3c  ead>***<Ver>1.0<
00064(00040) 2f 56 65 72 3e 0a 09 09 3c 53 6e 64 54 69 6d 65  /Ver>***<SndTime
00080(00050) 3e 30 3c 2f 53 6e 64 54 69 6d 65 3e 0a 09 09 3c  >0</SndTime>***<
00096(00060) 4d 73 67 4e 6f 3e 4c 30 30 31 30 31 3c 2f 4d 73  MsgNo>L00101</Ms
00112(00070) 67 4e 6f 3e 0a 09 09 3c 4e 6f 6d 42 61 6e 6b 49  gNo>***<NomBankI
00128(00080) 44 3e 30 30 30 31 3c 2f 4e 6f 6d 42 61 6e 6b 49  D>0001</NomBankI
00144(00090) 44 3e 0a 09 09 3c 4d 73 67 49 44 3e 44 48 43 43  D>***<MsgID>DHCC
00160(000a0) 30 33 30 34 30 30 30 30 30 35 35 33 32 38 3c 2f  03040000055328</
00176(000b0) 4d 73 67 49 44 3e 0a 09 09 3c 4f 72 69 67 4d 73  MsgID>***<OrigMs
00192(000c0) 67 49 44 3e 3c 2f 4f 72 69 67 4d 73 67 49 44 3e  gID></OrigMsgID>
00208(000d0) 0a 09 3c 2f 48 65 61 64 3e 0a 09 3c 42 6f 64 79  **</Head>**<Body
00224(000e0) 3e 0a 09 09 3c 42 61 73 65 49 6e 66 6f 3e 0a 09  >***<BaseInfo>**
00240(000f0) 09 09 3c 54 78 6e 49 44 3e 31 31 30 3c 2f 54 78  **<TxnID>110</Tx
00256(00100) 6e 49 44 3e 0a 09 09 09 3c 52 65 66 54 78 6e 49  nID>****<RefTxnI
00272(00110) 44 3e 30 3c 2f 52 65 66 54 78 6e 49 44 3e 0a 09  D>0</RefTxnID>**
00288(00120) 09 09 3c 49 6e 74 65 72 42 61 6e 6b 49 44 3e 37  **<InterBankID>7

解析代码如下:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
disp(char *p);
int main(int argc,char **argv)
{
    FILE *fp;
    char buf[100],*p;
    p=(char *)malloc(sizeof(buf));
    if((fp=fopen("hex","r"))==NULL)
    {
        printf("open error!
");
        exit(0);
    }
    memset(buf,0,sizeof(buf));
    while(fgets(buf,sizeof(buf),fp)!=NULL)
    {
        if((p=strstr(buf," "))!=NULL)
        {
            p++;
        }
        disp(p);
        memset(buf,0,sizeof(buf));
    }
    return 0;
}
disp(char *p)
{
    char b[2];
    memset(b,0,sizeof(b));
    while(p!=NULL)
    {
        
        sscanf(p,"%hhx",b);//hhx表示一个8位的十六进制数
        printf("%s",b);
        if((p=strstr(p," "))!=NULL)
        {
            p++;
            if(strncmp(p," ",1)==0)
                {
                    p++;
                    if(strncmp(p," ",1)==0)
                    exit(0);
                }
        }
    }
}
原文地址:https://www.cnblogs.com/sherlockhomles/p/3237810.html