Binfile conversion to hex file C language implementation,

Source: Internet
Author: User

Binfile conversion to hex file C language implementation,

For embedded systems, hex files may not be familiar to anyone anymore. For example, the code written by 51 Single-Chip Microcomputer we learned in college is compiled on keil and the hex file is generated. What does a bind file mean? What is the difference between it and hex files? This is not the focus of this article. The following is a brief description:

In most general terms, hex has an address. When downloading a file, you do not need to set an offset address. It is in the file stream format and is a standard ASCII code. The binfile does not contain an address, and all data streams are binary data streams. It is actually our so-called machine code. If you are interested, you can try to use disassembly to get the assembly code. The Code Compiled by the Development Board S3C2440 on ADS1.2 is the bin format stream. When opening a file with j-flash, you need to enter the offset address. The flash offset address of the Samsung platform is 0, the flash offset address of the stm32 platform is 0x08000000.

The data format of the hex file should have been described in the next article. In fact, there are many data formats in Baidu. The next one is the hex file converted to the binfile, which is just the opposite of this article. After talking about this, the following code is directly posted. If you have any details, you can leave a message for me. You are also welcome to spray me.

The code is implemented on VC6.0:

First create a bin2hex. h file

# Ifndef BIN2HEX_H # define BIN2HEX_Htypedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned long uint32_t; /*************************************** **************************************** * That is, each time the binfile is read and written N Bytes, then it is converted to a hex stream. The hex stream length calculation method is as follows: + Length + address + Type + N data records (N> = 0) + verification 1 + 2 + 4 + 2 + N ** 2 + 2 ************************* **************************************** * **************/# define NUMBER_OF_ONE_LINE0x20 # defineMAX_BUFFER_OF_ONE_LINE (NUMBER_OF_ONE_LINE * 2 + 11) typedef struct {uint8_t len; uint8_t addr [2]; uint8_t type; uint8_t * data ;}hexformat; typedef enum {RES_ OK = 0, // The RES_BIN_FILE_NOT_EXIST, // The binfile does not exist, including incorrect input paths. RES_HEX_FILE_PATH_ERROR // the target file path may be incorrectly entered.} RESULT_STATUS; RESULT_STATUS BinFile2HexFile (char * src, char * dest); # endif

Create a bin2hex. c file

# Include "bin2hex. h "# include <stdio. h> /************************************* **************************************** * ** input: dest: The converted result p-> addr [0]: High address p-> addr [1]: Low address p-> type: record type p-> data: valid Data Pointer for the bin format stream p-> len: the length of valid data for the bin format stream output: returns the length of valid data *********************************** **************************************** * ***/uint16_t BinFormatEncode (uint8_t * dest, hexFormat * p) {uint16_t offset = 0; uint8 _ T check = 0, num = 0; // :( 1) + Length (2) + address (4) + type (2) sprintf (& dest [offset], ": % 02X % 02X % 02X % 02X ", p-> len, p-> addr [0], p-> addr [1], p-> type ); offset + = 9; // hex format Stream Data Pointer offset 2 check = p-> len + p-> addr [0] + p-> addr [1] + p-> type; // calculate the checksum while (num <p-> len) // when the data length is not 0, add data in the previous hex format stream {sprintf (& dest [offset], "% 02X", p-> data [num]); check + = p-> data [num]; // calculate the checksum + = 2; // 2num ++ offset of Data Pointer in hex format; // next character} check = ~ Check + 1; // anticode + 1 sprintf (& dest [offset], "% 02X", check); offset + = 2; return offset; // return the hex format data stream length} RESULT_STATUS BinFile2HexFile (char * src, char * dest) {FILE * src_file, * dest_file; uint16_t tmp; HexFormat gHexFor; uint32_t low_addr = 0, hign_addr = 0; uint8_t buffer_bin [partition], buffer_hex [partition]; uint32_t src_file_length; uint16_t src_file_quotient, cur_file_page = 0; uint8_t src _ File_remainder; src_file = fopen (src, "rb"); // The source file is a binfile, which is opened in binary format if (! Src_file) // This is also used to check whether your input is prepared {return RES_BIN_FILE_NOT_EXIST;} dest_file = fopen (dest, "w"); // the target file is a hex file, open if (! Dest_file) {return future;} fseek (src_file, 0, SEEK_END); // locate src_file_length = ftell (src_file) at the end of the file; fseek (src_file, 0, SEEK_SET ); // locate the start point again and prepare to start reading data src_file_quotient = (uint16_t) (src_file_length/NUMBER_OF_ONE_LINE); // The number of times src_file_remainder = (uint8_t) that needs to be read) (src_file_length % NUMBER_OF_ONE_LINE); // remainder, the last number of characters required for gHexFor. data = buffer_bin; // point to the bin data stream to be converted while (cur_file_page <src_file_q Uotient) {fread (buffer_bin, 1, NUMBER_OF_ONE_LINE, src_file); gHexFor. len = NUMBER_OF_ONE_LINE; if (low_addr & 0xffff0000 )! = Hign_addr & hign_addr! = 0) // The Extended Linear address is written only after 64 K. For the first time, it is generally not {hign_addr = low_addr & 0xffff0000; gHexFor. addr [0] = (uint8_t) (hign_addr & 0xff000000)> 24); gHexFor. addr [1] = (uint8_t) (hign_addr & 0xff0000)> 16); gHexFor. type = 4; gHexFor. len = 0; // record extension address tmp = BinFormatEncode (buffer_hex, & gHexFor); fwrite (buffer_hex, 1, tmp, dest_file); fprintf (dest_file, "\ n") ;;}ghexfor. addr [0] = (uint8_t) (low_addr & 0xff00)> 8); gHexFor. ad Dr [1] = (uint8_t) (low_addr & 0xff); gHexFor. type = 0; // data record tmp = BinFormatEncode (buffer_hex, & gHexFor); fwrite (buffer_hex, 1, tmp, dest_file); fprintf (dest_file, "\ n ");; cur_file_page ++; low_addr + = NUMBER_OF_ONE_LINE;} if (src_file_remainder! = 0) // The number of last reads is not 0, which continues to read {fread (buffer_bin, 1, src_file_remainder, src_file); gHexFor. addr [0] = (uint8_t) (low_addr & 0xff00)> 8); gHexFor. addr [1] = (uint8_t) (low_addr & 0x00ff); gHexFor. len = src_file_remainder; gHexFor. type = 0; // data record tmp = BinFormatEncode (buffer_hex, & gHexFor); fwrite (buffer_hex, 1, tmp, dest_file); fprintf (dest_file, "\ n") ;;}ghexfor. addr [0] = 0; gHexFor. addr [1] = 0; gHexFor. type = 1; // The end character gHexFor. len = 0; tmp = BinFormatEncode (buffer_hex, & gHexFor); fwrite (buffer_hex, 1, tmp, dest_file); fprintf (dest_file, "\ n ");; fclose (src_file); fclose (dest_file); return RES_ OK ;}

Create a new main. c file with parameters. This file is mainly used for batch processing and is for another purpose.

#include <stdio.h>#include "bin2hex.h"int main(int argc, char *argv[]){RESULT_STATUS res;if (argc != 3){printf("input para doesn't match\r\n");return -1;}res = BinFile2HexFile(argv[1], argv[2]);switch (res){case RES_OK:printf("hex file to bin file success!\r\n");return -1;case RES_BIN_FILE_NOT_EXIST:printf("bin file doesn't exist!\r\n");return -1;case RES_HEX_FILE_PATH_ERROR:printf("hex file path is error, please check it!\r\n");return -1;}return 0;}

Compile and generate the bin2hex. c file for the three source files.

The usage is described below:

Copy the bin2hex. c file to the root directory of the c drive, and then copy the binfile to be converted. Here I copied an hwb. binfile. Then click the menu to start-> Run-> Enter cmd-> enter the dos window-> adjust the current directory to c:. If you do not know this, you can click Baidu. The command cd cannot be used.

Finally, enter the command bin2hex hwb. bin hwb. after hex is entered, you can see the prompt that the conversion is successful. check whether there is a hex file. The code in this article can be larger than 64 KB, you can download the converted hex to the single-chip microcomputer to run it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.