怎么用c语言解析二进制文件
1个回答
展开全部
//ver:1
//resv1:0
//signature:CUC
//type:69
//no:5
//resv2:0
//seq:4
//length:56
//int2,1
//int2,2
//int1,1
//int4,30
//int1,1
//int1,7
//str,beijing
//int2,1
//int2,4
//int1,1
//int4,60
//int1,1
//int1,7
//str,tianjin
//int4,80
//二进制文件b.bin是:
//00000000h: 01 43 55 43 45 00 05 00 00 00 00 04 00 00 00 38
//00000010h: 00 01 00 02 01 00 00 00 1E 01 07 62 65 69 6A 69
//00000020h: 6E 67 00 01 00 04 01 00 00 00 3C 01 07 74 69 61
//00000030h: 6E 6A 69 6E 00 00 00 50
#pragma comment(lib,"ws2_32")
#include <stdio.h>
#include <winsock2.h>
#pragma pack(push,1)
struct _D {
char ver;
// char resv1;
char signature[3];
char type;
short no;
char resv2;
int seq;
int length;
short int2_0;
short int2_1;
char int1_0;
int int4_0;
char int1_1;
char int1_2;
char str_0[7];
short int2_2;
short int2_3;
char int1_3;
int int4_1;
char int1_4;
char int1_5;
char str_1[7];
int int4_2;
} d;
#pragma pack(pop)
FILE *f;
int main() {
f=fopen("b.bin","rb");
if (NULL==f) {
printf("Can not open file b.bin!\n");
return 1;
}
fread(&d,sizeof(struct _D),1,f);
fclose(f);
printf("ver:%d\n" ,d.ver);
printf("resv1:0\n");
printf("signature:%.3s\n",d.signature);
printf("type:%d\n" ,d.type);
printf("no:%hd\n" ,ntohs(d.no));
printf("resv2:%d\n" ,d.resv2);
printf("seq:%d\n" ,ntohl(d.seq));
printf("length:%d\n" ,ntohl(d.length));
printf("int2_0:%hd\n" ,ntohs(d.int2_0));
printf("int2_1:%hd\n" ,ntohs(d.int2_1));
printf("int1_0:%d\n" ,d.int1_0);
printf("int4_0:%d\n" ,ntohl(d.int4_0));
printf("int1_1:%d\n" ,d.int1_1);
printf("int1_2:%d\n" ,d.int1_2);
printf("str_0:%.7s\n" ,d.str_0);
printf("int2_2:%hd\n" ,ntohs(d.int2_2));
printf("int2_3:%hd\n" ,ntohs(d.int2_3));
printf("int1_3:%d\n" ,d.int1_3);
printf("int4_1:%d\n" ,ntohl(d.int4_1));
printf("int1_4:%d\n" ,d.int1_4);
printf("int1_5:%d\n" ,d.int1_5);
printf("str_1:%.7s\n" ,d.str_1);
printf("int4_2:%d\n" ,ntohl(d.int4_2));
return 0;
}
//ver:1
//resv1:0
//signature:CUC
//type:69
//no:5
//resv2:0
//seq:4
//length:56
//int2_0:1
//int2_1:2
//int1_0:1
//int4_0:30
//int1_1:1
//int1_2:7
//str_0:beijing
//int2_2:1
//int2_3:4
//int1_3:1
//int4_1:60
//int1_4:1
//int1_5:7
//str_1:tianjin
//int4_2:80
//
代码示例
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询