⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hex2bin.txt

📁 c51经典子程序代码全集
💻 TXT
字号:
闲聊:应铜梁的要求,自己写了个HEX2BIN,特贴出来丢人现眼。 [刀客] [103次] 01-9-17 下午 05:32:04
//Title: Hex2bin
//Test by Borland C++ 3.1 and Microsoft Visual C++ 1.0
//Author: dock

#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <process.h>    // for exit()
#include <fstream.h>    // for ifstream, ofstream
#define MAXLENTH 77     // 32 bytes valid record in a line
#define hexval(c) ((c) >= '0' && (c) <= '9' ?  \
                     (c) - '0' :               \
                   (c) >= 'A' && (c) <= 'F' ?  \
                     (c) - 'A' + 10 :          \
                   (c) >= 'a' && (c) <= 'f' ?  \
                     (c) - 'a' + 10 :          \
                     -1                      )

unsigned char getcode(void);   // combine a byte and increase the pointer.

unsigned char linebuff[MAXLENTH],*bufptr;
                     
main(int argc, char* argv[])  // access command-line arguments
{
   char fnami[15],fnamo[15]; // fnami = input file name etc 
   unsigned int Line=0;    // Program Counter , Source File line
   unsigned char arec,attrib,areclen,temp,checksum;
   long  offset;           // file offset at writting
   char ch,validline;
   if (argc != 2)      // test number of arguments
   {
      cerr << "USAGE: Hexbin file(.hex)\n";
      exit(-1);
   }
   strcpy(fnami,argv[1]);  // get first part of filename from cmd line 
   strcat(fnami,".hex");   // add the extension to the filename string 

   strcpy(fnamo,argv[1]);  // same for o/p filename 
   strcat(fnamo,".bin");

   ifstream source;    // declare input and output streams
   ofstream dest;

   source.open(fnami,ios::nocreate); // source file must be there
   if (!source)
   {
      cerr << "Cannot open source file " << fnami << " for input\n";
      exit(-1);
   }
   dest.open(fnamo,ios::binary);   // dest file will be created if not found
                       // or cleared/overwritten if found
   if (!dest)
   {
      cerr << "Cannot open destination file " << fnamo << " for output\n";
      exit(-1);
   }
   
   validline=0;
   while (!source.eof()) {
     source.get(ch);
     if(ch==':')validline=1;    // A valid record must be started with :
     else if(ch=='\t'){validline=0;Line++;}
     else validline=0;  // Omit the comment
     if(validline){
         bufptr=linebuff;
         source.get(bufptr,int(MAXLENTH),'\n');
        Line++;
//Do the Checksum
        checksum=0;  temp=1;
    do{ *bufptr=hexval(*bufptr);
          temp++;
          if(temp%2)checksum+= *bufptr + *(bufptr-1)*16;
        }while(*++bufptr);
//        checksum+=*(bufptr-1);  
    if(checksum){
           cerr<<"Checksum in Line " << Line << " is Error.\n";
           exit(2);
    }

    bufptr=linebuff;
        areclen=getcode();       //First 2 bytes are record length
        offset=getcode();        //Second 4 bytes are record offset
         offset=(offset&0xff)<<8; // Avoid sign extension 
        offset|=getcode();
        attrib=getcode();        //If attrib = 1 then the file end. 
        if(attrib){ cout<< "Hexbin completed\n"; dest.close(); exit(0); }
         dest.seekp(offset);
        for(temp=0;temp<areclen;temp++) dest.put(getcode());
     }
   }
   
   cout << "Warning: unrecognized file end\n";

   source.close();        // close both streams
   dest.close();
}

unsigned char getcode(){
  unsigned char i;
  i=*bufptr++;
  i=i<<4;
  i|=*bufptr++;
  return i;
}


可别这么说,欢迎你多“丢人显眼”几回呵呵。 [zilingzhang] [0次] 01-9-17 下午 05:38:04 

谢谢!我没用个C++,该程序TC能编译吗? [铜梁] [3次] 01-9-17 下午 08:05:20 

TC3.0应该可以,可执行程序我放到上载区了。 [刀客] [9次] 01-9-17 下午 08:12:13 
填充FF没有实现,其实你只要看得懂,就很容易改。
最笨的办法是把处理HEX的那一段重新拷贝一份,处理两遍,第一遍找到最大的那个offset
及其对应的areclen,两个量相加就是最终BIN文件长度,然后把dest流注入这么多FF即可。
第二遍保留现在的即可。 

好,我要另存为。。,好好消化。 [keilc51] [1次] 01-9-17 下午 10:04:12 

 
点击这里回复这篇贴子>>
_____________________________________________________________________________
Copyright?,C51BBS论坛 2000-2001 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -