HEX to BIN source code analysis (51 series)

Publisher:平凡梦想Latest update time:2016-12-15 Source: eefocusKeywords:HEX  BIN Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  I wrote a download program for Atmel's S5X before, which supports HEX format files, so I posted this program. The program means to convert the input HEX file into a BIN format file and store it in the file. Note that it does not support 64K extension mode.



int CFlashP51App::HexToBin(CString hexfile, CString binfile)

{

    CFile fex, fbin;

    CString pBuffer;

    BYTE len, len1, len2, len_at_max=0;

    int start_addr, start_addr_max=0;

    int nEnd, n, bin_length=0;


    if(!(fhex.Open(hexfile, CFile::modeRead, NULL))) //// Open the file

        return -1;

    if(!(fbin.Open(binfile, CFile::modeCreate|CFile::modeWrite, NULL)))

        return -1;

    

    char *buffer = new char [fhex.GetLength()]; //// Allocate memory

    fhex.Read(buffer, fhex.GetLength());

    pBuffer = buffer;


    nEnd = pBuffer.Find(":00000001"); //// Use this to determine if the file format is HEX

    if(nEnd < 0) //First determine the end of the file

    {

     // AfxMessageBox("*.Hex file conversion failed (\":00000001\" record not found)!");

        return -2;

    }

    n = 0;

    while(n < nEnd) // First get the length of the bin file

    {

        if(buffer[n] == ':')

        {

            CharToByte(&buffer[n+1], &len);

            CharToByte ( & buffer [ n + 3 ] , & len1 ) ;

            CharToByte(&buffer[n+5], &len2);

            start_addr = (len1<<8) + len2; // starting address

            /*

             * Some HEX files have "holes" in the middle, and only the size of the previous row of data is calculated.

             * Errors may occur, so get the maximum address value and the row with the maximum address value

             * The size of the BIN file can be obtained by using the data size. In fact, this method should be used accurately.

             * The method of the surface is not removed, it can be removed

             *

             */

            if(start_addr >= start_addr_max)

            {

                start_addr_max = start_addr;

                len_at_max = len;

            }

            bin_length += len;

            n += len*2+11; // No carriage return symbol

        }

        else

            n++;

    }

    start_addr_max += len_at_max;

    if(start_addr_max >= bin_length)

        bin_length = start_addr_max;


    BYTE *bbuf = new BYTE [bin_length]; //// Allocate memory

    for(int i=0; i

        bbuf[i] = 0xff;

    n = 0;

    while(n < nEnd) // Write data to the BIN file buffer

    {

        if(buffer[n] == ':')

        {

            CharToByte(&buffer[n+1], &len);

            CharToByte ( & buffer [ n + 3 ] , & len1 ) ;

            CharToByte(&buffer[n+5], &len2);

            start_addr = (len1<<8) + len2; // starting address    

            for(unsigned int i=0; i

            {

                CharToByte(&buffer[n+9+i*2], &len1);

                bbuf[start_addr+i] = len1;

            }

            n += len*2+11; // No carriage return symbol

        }

        else

            n++;

    }

    fbin.Write(bbuf, bin_length);


    fhex.Close();

    fbin.Close();

    delete [] buffer;

    delete [] bbuf;

    return bin_length;

}


BOOL CFlashP51App::CharToByte(char *pChar, BYTE *pByte)

{

    char h,l;

    h = pChar[0]; //high 4 bits

    l = pChar[1]; //lower 4 bits

    if(l>='0' && l<='9')

        l = l - '0';

    else if(l>='a' && l<='f')

        l = l - 'a' + 0xa;

    else if(l>='A' && l<='F')

        l = l - 'A' + 0xa;

    else

        return FALSE;


    if(h>='0' && h<='9')

        h = h - '0';

    else if(h>='a' && h<='f')

        h = h - 'a' + 0xa;

    else if(h>='A' &&h <='F')

        h = h - 'A' + 0xa;

    else

        return FALSE;

    *pByte = (BYTE)h*16 + l;

    return TRUE;

}


 


Keywords:HEX  BIN Reference address:HEX to BIN source code analysis (51 series)

Previous article:Homemade MCU Part 4… LCD1602 Driver
Next article:The MCU program emulator cannot run normally after being correctly burned

Recommended Content
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号