Convert bin file to hex file C language implementation

Source: Internet
Author: User
Tags fread sprintf

For the embedded, the hex file may be familiar to everyone, yes, we study in the University of the 51 single-chip computer code compiled on the Keil generated after the hex file. What does that bin file mean, and what's the difference between it and the hex file? This is not the focus of this article, the following simple description:

Most commonly speaking, Hex is with the address, when downloading with the downloader, do not need to set the offset address, it is the file stream format, is the standard ASCII code. And the bin file is no address, all binary data stream, stop, is actually what we call the machine code. Interested classmates, can try to use disassembly, get is the assembly code. I use the Development Board s3c2440 on the ADS1.2 compiled code is the bin format stream, with J-flash open the file when you need to fill in the offset address, Samsung platform Flash offset address is 0, and stm32 platform Flash offset address is 0x08000000.

Originally should be to describe the data format of the hex file, this left the next article to describe, in fact, there are many Baidu. The next one is the hex file converted to a bin file, just as opposed to this article. Said so much, the following directly posted code, there is not detailed can give me a message, but also welcome everyone to spray me.

The code is implemented on VC6.0:

Create a new bin2hex.h file first

#ifndef bin2hex_h#define bin2hex_htypedef unsigned char uint8_t;typedef unsigned short uint16_t;typedef unsigned long UIn t32_t;/******************************************************************************** is every time you read and write Bin file n bytes, Then convert to hex format stream, hex format Stream Length calculation method: + length + address + type + N data (n >= 0) + checksum 1 + 2 + 4+ 2   + N * 2 + 2****************************** /#define Number_of_one_line0x20#definemax_buffer_of_one_line ( Number_of_one_line * 2 + one) typedef struct {uint8_t len;uint8_t addr[2];uint8_t type;uint8_t *data;} hexformat;typedef enum {RES_OK = 0,//operation completed res_bin_file_not_exist,//equivalent to the BIN file does not exist, including the input path may be incorrect res_hex_file_path_ error//destination file path may be entered incorrectly} Result_status; Result_status Binfile2hexfile (Char *src, char *dest); #endif

New bin2hex.c file

#include "bin2hex.h" #include <stdio.h>/******************************************************************* Input:dest: For converted results p->addr[0]: High address p->addr[1]: Low address p->type: Record type P->data: Stream valid data pointer for bin format p-> Len: Stream valid data length for bin format 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); o Ffset + = 9;//hex format stream data pointer offset 2check = P->len + p->addr[0] + p->addr[1] + p->type;//COMPUTE checksum while (num < P->len) When the data length is not 0, continue to add data in the previous hex format stream {sprintf (&dest[offset], "%02x", P->data[num]); check + = p->data[num];// Calculates the checksum offset + = 2;//hex Format Data flow data pointer offset 2num++;//next character}check = ~check + 1;//anti-code +1sprintf (&dest[offset], "%02x", check); o Ffset + = 2;return offset;//Returns the length of the hex format data stream}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[number_of_one_line], Buffer_hex[MAX_BUFFER_OF _one_line];uint32_t src_file_length;uint16_t src_file_quotient, cur_file_page = 0;uint8_t src_file_remainder;src_ File = fopen (src, "RB");//source file is BIN file, open if (!src_file) in binary form///Here is also equivalent to check whether the user's input is prepared {return res_bin_file_not_exist;} Dest_file = fopen (dest, "w");//The destination file is a HEX file, open if (!dest_file) {return res_hex_file_path_error in the form of text); Fseek (src_file, 0, seek_end);//Locate to the end of the file Src_file_length = Ftell (src_file); fseek (src_file, 0, Seek_set);//reposition to the beginning, Ready to start reading data Src_file_quotient = (uint16_t) (src_file_length/number_of_one_line);//quotient, how many times need to be read Src_file_remainder = (uint8_ T) (src_file_length% number_of_one_line);//remainder, how many characters are required for the last time Ghexfor.data = buffer_bin;//points to the bin data stream that needs to be converted while (Cur_file_ Page < src_file_quotient) {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)//Only after more than 64K write extended linear address, the first time is generally not {hign_addr = low_addr & 0xffff0000;ghexfor.addr[0] = (uint8_t) (( HIGN_ADDR & 0xff000000) >> 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.addr[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;//Terminator Ghexfor.len = 0;tmp = Binformatencode (Buffer_hex, &gHex for); Fwrite (Buffer_hex, 1, TMP, dest_file); fprintf (Dest_file, "\ n"); fclose (src_file); fclose (dest_file); return Res_ OK;}

New Main.c file, here is with parameters, mainly for the convenience of batch processing, is 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"); Break;case R es_bin_file_not_exist:printf ("BIN file doesn ' t exist!\r\n"); Break;case res_hex_file_path_error:printf ("HEX file PATH Is the error, please check it!\r\n "); return 0;}

Compile and build the Bin2hex.c file for three source files.

Below is a description of the following usage:

Copy the bin2hex.c file to the C packing directory, and then copy a bin file that needs to be converted, here I copy a hwb.bin file. Then click on the menu start, run-and input cmd-> into the DOS window, adjust the current directory as C:, this if you do not know Baidu can be a bit, cannot be the command CD.

Last input command: Bin2Hex hwb.bin hwb.hex, after input, you can see the prompt said conversion success, we check whether there is a hex file, the code support for this article is greater than 64K, we will convert the hex download to a single-chip computer run try.

Convert bin file to hex file C language implementation

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.