Data sending and receiving verification is often required for communication projects. Data on the sender end is intertwined, encoded (low-density inspection), mapped, IFFT, and serial-parallel conversion through complex communication systems (such as OFDM systems ...... And so on. To achieve the acceptor, we can verify the correctness of the process only through repeated comparisons. Because the data is sent and received by bit streams, 16 or binary files are often used for comparison.
Many debugging tools support data export. For some reason, they support different export formats. For example, Code Composer Studio supports a comprehensive set of data formats. However, the formats of niosii ide data can only be exported in hexadecimal notation, and the formats of quarutus II are also different. For example, we need to compare the following two files:
1. Source File
0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A0102030405060708090A
This file is a hexadecimal file, in essence 0x01 0x02 0x03 ...... This hexadecimal data.
2. Target File
01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A 01 02 03 04 05 06 07 08 09 0A
The target file may contain spaces and a 0x prefix, such as 0x01020304 0x05060708. So how can we compare them. The following is a C script file that can be implemented. The function includes hexadecimal comparison and binary comparison.
#include <stdio.h>#include <stdlib.h>#include <string.h>long filesize(FILE *stream){ long curpos, length; curpos = ftell(stream); fseek(stream, 0L, SEEK_END); length = ftell(stream); fseek(stream, curpos, SEEK_SET); return length;}int main(int argc, char **argv){ FILE *fp_input; FILE *fp_output; unsigned long flength = 0L; char *str_file = NULL; unsigned long i = 0L; unsigned long out_size = 0L; char byte_data = 0; char binary_mode = 0; char byte_temp = 0; char hex_byte = 0x00; int j = 0; /* check usage*/ if (argc < 3) { fprintf(stderr, "hex_export version-1.0 2014-06-27 [email protected]\nto use this tool:\nhex :%s [input hex filename] [output hex file name]\n", argv[0]); fprintf(stderr, "binary:%s [input hex filename] [output hex file name] -b \n", argv[0]); exit(-1); } if(argc == 4){ binary_mode = 1; printf("convert hex into binary\n"); } /*open the specified file*/ if((fp_input = fopen(argv[1],"r")) == NULL){ printf("Can‘t open intput file: %s \n",argv[1]); } if((fp_output = fopen(argv[2],"w")) == NULL){ printf("Can‘t open output file: %s \n",argv[2]); } /*get file size*/ flength = filesize(fp_input); if((str_file = (char *)malloc(flength)) == NULL){ fprintf(stderr,"Can‘t allocate memory for file input!\n"); exit(-1); } /*read file*/ if(fread(str_file, 1, flength, fp_input) == 0){ fprintf(stderr,"Can‘t read file!\n"); exit(-1); } /* do extract hex */ for(i=0; i<flength; i++){ if(*(str_file+i) == ‘0‘ && (toupper(*(str_file+i+1)) == ‘X‘)){ /* just omit*/ }else if(isxdigit(*(str_file+i))){ byte_data = toupper(*(str_file+i)); if(byte_data >= ‘0‘ && byte_data <= ‘9‘){ hex_byte = byte_data - 0x30; }else{ hex_byte = byte_data - ‘A‘ + 10; } //byte_data = *(str_file+i); if(binary_mode){ for(j=0; j<4; j++){ byte_temp = (hex_byte&(0x1<<(3-j)))>>(3-j); byte_temp += ‘0‘; fprintf(fp_output,"%c\n",byte_temp); } out_size++; }else{ fprintf(fp_output,"%c\n",byte_data); out_size++; } } } free(str_file); fclose(fp_input); fclose(fp_output); /*message*/ printf("Input file is : %s size = %ld\n", argv[1],flength); printf("Output file is : %s size = %ld\n",argv[2],out_size); return 0;}
3. Usage
For example, compile the above program as: hex_cmp
Hex_cmp src_input.dat src_output.dat: by default, the file is compared in hexadecimal notation, and converted to the column mode in 4 bit units. Then, it is very convenient to use tools such as beyond compare.
To compare binary files, use the following method:
Hex_cmp src_input.dat src_output.dat-B: converts it to a binary bit stream column file, and then it is very convenient to use tools such as beyond compare.