把打包好的hex格式的文件转化成bin文件格式

来源:互联网 发布:当前世界危机知乎 编辑:程序博客网 时间:2024/06/17 21:57

需求,keil生成的是hex格式的文件,我要对该文件添加我们公司的相关信息,并且提供一个函数,在升级的时候可以吧hex格式的文件转化成bin格式下载到开发板上,要求是这个函数有两个地址指针参数,返回一个bin格式文件的大小

//开启一个线程

CreateThread((LPSECURITY_ATTRIBUTES)NULL,0,(LPTHREAD_START_ROUTINE)deal_function,this,0,NULL);

//线程中的代码

Chex_to_binDlg* p = (Chex_to_binDlg*)lpParm;

 CStdioFile myFile;
 CFileException fileException;
 CString prefilename;
 CString savefilename;
 int res;
 p->m_prefilename.GetWindowTextW(prefilename);
 p->m_savename.GetWindowTextW(savefilename);
 //申请两个空间
 char * src_buf = new char[SIZE_BUF];
 char * des_buf = new char[SIZE_BUF];
 memset(src_buf,0,sizeof(src_buf));
 memset(des_buf,0,sizeof(des_buf));
 if(myFile.Open(prefilename, CFile::typeBinary|CFile::modeRead,&fileException))
 {
  UINT res=-1;
  res = myFile.Read(src_buf,SIZE_BUF);
  if(!res)
  {
   p->MessageBox(_T("读文件操作失败!"));
   delete[] src_buf;
   delete[] des_buf;
   kk=0;
   return FALSE;
  }
  src_buf[res]='\0';
  myFile.Close();
 }
 else{
  p->MessageBox(_T("打开读文件失败!"));
  delete[] src_buf;
  delete[] des_buf;
  kk=0;
  return FALSE;
 }
 
 //把hex文件转化成bin文件
 res = hex_to_bin(des_buf,src_buf);
 //res = hex_to_bin();
 if(res<0)
 {
  p->MessageBox(L"hex不是需要的文件!");
  delete[] src_buf;
  delete[] des_buf;
  kk=0;
  return FALSE;
 }
 //把目标地址中写到文件中去
 if(myFile.Open(savefilename, CFile::modeCreate|CFile::typeBinary|CFile::modeWrite,&fileException))
 {
  myFile.Write(des_buf,res);
  myFile.Close();
 }
 else{
  p->MessageBox(_T("打开写文件失败!"));
  delete[] src_buf;
  delete[] des_buf;
  kk=0;
  return FALSE;

 

//转化函数的实现

int hex_to_bin(char *des, char *scr)
//int hex_to_bin()
{
 //char *des_temp = des_buf;
 //char *scr_temp = src_buf;
 char *des_temp = des;
 char *scr_temp = scr;


 int filesize = 0;
 char filesize_buf[50] = {0};
 char data[2] = {0};
 int i;
 int p=0;
 unsigned char cc;
 BYTE bin_data;
 int size_des = 0;;

 filesize = strlen(scr);
 //filesize = strlen(src_buf);


 // 读出版本信息,格式“:F0 + 8个字节+\r\n”,共13个字节
 if((*scr_temp++==':') && (*scr_temp++=='F')&&(*scr_temp++=='0'))
 {
  scr_temp+=10;
 }
 else{
  return -1;
 }
 p+=13;
 //读出文件名称,格式“:F1+(<=32)个字节+\r\n”
 if((*scr_temp++==':')&&(*scr_temp++=='F')&&(*scr_temp++=='1'))
 {
  p+=3;
  do
  {
   scr_temp++;
   p++;
  } while (*scr_temp!=':');
 }
 else{
  return -1;
 }
 //读出文件大小,格式“:F2 + 8个字节\r\n”,共13个字节
 if((*scr_temp++==':')&&(*scr_temp++=='F')&&(*scr_temp++=='2'))
 {
  p+=3;
  for(i=0; ;i++)
  {
   if(*scr_temp=='\r') break;
   data[0] = *scr_temp++;
   data[1] = *scr_temp++;
   p+=2;
   char_to_byte(data,&bin_data);
   filesize_buf[i] = bin_data;
  }
  scr_temp+=2;
  p+=2;
 }
 else{
  return -1;
 }

 if((filesize-p)!=atoi(filesize_buf))
 {
  return -1;
 }

 // 把hex中的二进制数转化成bin文件的二进制数
 // hex格式的文件中 “:+ 2个字符的数据个数+4个字符的地址+2个字节的类型+数据+2个字节的校验”
 //转化成bin文件时,只需要转化其中32个字节的数据就行了,没有32个字节的行都不需要
 
 for(;;)
 {
  i++;
  if(*scr_temp++==':')
  {
   if((*scr_temp++=='1')&&(*scr_temp++=='0'))
   {
    //前8个字节不需要
    cc = 0x10;
    
    for(i=0;i<3;i++)
    {
     data[0]=*scr_temp++;
     data[1]=*scr_temp++;
     char_to_byte(data,&bin_data);
     cc+=bin_data;
    }
    for(i=0; i<16;i++)
    {
     data[0] = *scr_temp++;
     data[1] = *scr_temp++;
     char_to_byte(data,&bin_data);
     cc+=bin_data;
     *des_temp++=bin_data;
     size_des++;
    }

    cc = (unsigned char) ~(cc);
    cc++;
    data[0] = *scr_temp++;
    data[1] = *scr_temp++;
    char_to_byte(data,&bin_data);
    if(cc!=bin_data)
    {
     return -1;
    }
    scr_temp+=2;
    if(*scr_temp=='\0') return size_des;
    if(*scr_temp!=':')
    {
     return -1;
    }
   }
   else{
    /*for(;;)
    {
     scr_temp++;
     if(*scr_temp=='\0')
     {
      //scr_temp=NULL;
      int j=2;
      while(j--);
      return 0;
     }
     if(*scr_temp==':') break;
    }*/
    do{
     if(*scr_temp++=='\0')
     {
      //*des_temp = '\0';
      return size_des;
     }
    }while(*scr_temp!=':');
   }
  }
  
 }

 /*while(*scr_temp!='\0')
 {
  *des_temp++ = *scr_temp++;
 }
 *des_temp = '\0';*/

 return -1;
 
}

//将两个字符串转换成一个字符串
void char_to_byte(char* pChar, BYTE*pByte)
{
 char h,l;
 h = pChar[0];
 l = pChar[1];
 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;
 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;
 *pByte = (BYTE)h*16+l;
}