HEX2BIN脚本

#include <stdio.h>
#include <string.h>
#include <stdlib.h>//free


//完成'4'-->4  ‘A’-->10
unsigned char G_strTobyte(unsigned char dData)
{
	unsigned char rst=0;
	if((dData >= '0')&& (dData <= '9'))
				rst = dData-'0';
	else if((dData >= 'A')&&(dData <= 'F'))
				rst =  dData+10-'A';
	else if((dData >= 'a')&&(dData <= 'f'))
				rst =  dData+10-'a';
	else {printf("ERR G_strTobyte\r\n");rst = 0;}//不是ASCII的char就返回0 有点儿欠妥!
			
	return rst;
}
//压缩一半
//完成"10"--->0x10  "1234"--->0X12,0X34 
//len标识传入的strlen(strings)
//返回值是len的一半
char G_strsTobytes(void* Strings,void* Bytes,char len)
{
	unsigned char* strings=(unsigned char *)Strings;
	unsigned char* bytes  =(unsigned char *)Bytes;
	unsigned char i = 0,j=0,lowbits=0,highbits=0;
	if(len%2){printf("ERR G_strsTobytes\r\n");return 0;}//禁止奇数
	for (i = 0; i < len; i+=2)
	{           
		highbits = G_strTobyte(strings[i]  );
		lowbits  = G_strTobyte(strings[i+1]);
		bytes[j++] =( highbits << 4)|lowbits;
	}
	return j;
}

int main(int argc,char **argv)
{
	FILE *fp1,*fp2; 
	unsigned int lSize=0;
	char *name = NULL;
	unsigned char ch,start=0;
	unsigned char buf[44]={0},i=0,len=0,addr,type;
	unsigned char bin[16]={0},j=0;
	if(argc==1)
	{
		name = (char *)"1.bin";
		printf("NEWNAME %s\r\n",name);
	}
	else if(argc==2)
	{
		name = argv[1];
		printf("NEWNAME %s\r\n",name);
	}

	fp1 = fopen("1.hex", "r");
	if (NULL == fp1){ printf("NULL == fp1"); return 1;}

	fp2 = fopen(name, "wb");
	if (NULL == fp2){ printf("NULL == fp2");return 1;}
	

	while(1)
	{
		ch = fgetc(fp1);
		if(feof(fp1))		//如果读到了文件结尾,就退出 while循环
			break;
     
	    if(start == 0)
	    {
	    	if(ch==':'){start = 1;i=0;len=0;memset(buf,0,44);memset(bin,0,16);}
	    }	
		else 
		{
//  :1016500003F92802083010120878124408520418BE
//  :0E1660001003E529020848100002F8F80208FD
			buf[i++]=ch;
			if(i==2)//10
			{
			    len  = (G_strTobyte(buf[0]))*16 + (G_strTobyte(buf[1]));
				//源码这有问题比如上面0E就处理成21了 需要上面的函数 len  = (buf[0]-'0')*16 + (buf[1]-'0');
				//if(len!=16 && len!=8 && len!=0){start = 0;continue;}//要求长度是16 最后有个是8 其他的赶紧走
				//if(len ==2 || len==4 ){start = 0;continue;}//要求长度是16 最后有个是8 其他的赶紧走
				if(buf[0]=='1'&&buf[1]=='0')
				{}
				else 
				{printf("[%c-%c]\r\n",buf[0],buf[1]);}
			}
			if(i==2+4)//A640
			    addr = buf[2]+buf[3]+buf[4]+buf[5];
			if(i==2+4+2)//00
			{
				type = (buf[6]-'0')*16 + (buf[7]-'0');

				if(type!=0)//要求类别是00的才可以放入bin文件 别的赶紧走
				{ 
					//if(type==4)printf("不支持线下地址\r\n");	
					//if(type==2)printf("不支持扩展段\r\n");	
					//if(type==1)printf("文件即将结束\r\n");	
					printf("type=%d\r\n",type);	
					start = 0;
				}
			}
	        if(i == 2+4+2+len*2)
	        {//0208116A0208B9690208E96B84190C1E
 	        	G_strsTobytes(&buf[8],bin,len*2);
	        	for(j=0;j<len;j++)
					fputc(bin[j] , fp2);
				start = 0;
				i=0;
	        }

		}
	}
	
	fclose(fp1);
	fclose(fp2);
	free(fp1);
	free(fp2);
	printf("******GKOSON FINISHED*******");
	getchar();
	return 1;
}

此时可以work 之前的KEIL的都很好 一样

IAR我自己测一下

出来的BIN 不是4的倍数!!!

WHY??可能吗?

161390

蓝加提交

 

 

 

 

下面是别人的源码:

#include "stdio.h"
#include "string.h"

unsigned char ChartoByte(char c)
{
    if(c-'a'>=0 ) return(c-'a'+10);
    else if(c-'A'>=0 ) return(c-'A'+10);
    else return(c-'0');
}

unsigned char Char2toByte(char* s)
{
    return (ChartoByte(*s)*16+ChartoByte(*(s+1)));
}


int main(int argc,char *argv[])
{
    FILE *fp_hex = NULL;
    FILE *fp_bin = NULL;
    char buff[64] = "" ;

    unsigned char length = 0;
    unsigned short offset = 0;  //0~65535
    unsigned char type = 0;
    unsigned char checksum = 0;
    unsigned char i = 0;

    if(argc != 2) return -1;

    printf("Input file: %s\n",argv[1]);

    if((fp_hex=fopen(argv[1],"r"))==NULL)
    {
        printf("File open error!!!\n");
        return -1;
    }

    i=strlen(argv[1]);
    argv[1][i-1]='n';
    argv[1][i-2]='i';
    argv[1][i-3]='b';
    printf("Output file: %s\n",argv[1]);

    if((fp_bin=fopen(argv[1],"wb"))==NULL)
    {
        printf("File creat error!!!\n");
        return -1;
    }

    while(1)
    {
        fgets(buff,64,fp_hex);

        if(feof(fp_hex)) break; //文件结束
        else if(buff[0] != ':') continue; //无效行
        else if( strcmp(buff,":00000001FF\n") == 0  ) break; //结束行
        else
        {
            length=Char2toByte(&buff[1]);
            offset=Char2toByte(&buff[3])*256+Char2toByte(&buff[5]);
            type=Char2toByte(&buff[7]);
            if(type==0)
            {
                fseek(fp_bin,offset,0);
                for(i=0; i<length; i++)
                    fputc(Char2toByte(&buff[9+2*i]), fp_bin);
            }

            printf("%s",buff);
        }
    }



    return(0);
}

 

hex2bin v2.2, Copyrhex2bin v2.2, Copyright (C) 2015 Jacques Pelletier & contributorsight (C) 2015 Jacques Pelletier & contributors usage: hex2bin [OPTIONS] filename Options: -a Address Alignment Word (hex2bin only) -b Batch mode: exits if specified file doesn't exist -c Enable record checksum verification -C [Poly][Init][RefIn][RefOut][XorOut] CRC parameters -e [ext] Output filename extension (without the dot) -E [0|1] Endian for checksum/CRC, 0: little, 1: big -f [address] Address of check result to write -F [address] [value] Address and value to force -k [0-4] Select check method (checksum or CRC) and size -d display list of check methods/value size -l [length] Maximal Length (Starting address + Length -1 is Max Address) File will be filled with Pattern until Max Address is reached -m [size] Minimum Block Size File Size Dimension will be a multiple of Minimum block size File will be filled with Pattern Length must be a power of 2 in hexadecimal [see -l option] Attention this option is STRONGER than Maximal Length -p [value] Pad-byte value in hex (default: ff) -r [start] [end] Range to compute checksum over (default is min and max addresses) -s [address] Starting address in hex for binary file (default: 0) ex.: if the first record is :nn010000ddddd... the data supposed to be stored at 0100 will start at 0000 in the binary file. Specifying this starting address will put pad bytes in the binary file so that the data supposed to be stored at 0100 will start at the same address in the binary file. -v Verbose messages for debugging purposes -w Swap wordwise (low <-> high)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值