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

📄 bin2hex.txt

📁 Bin文件转Hex原代码
💻 TXT
字号:
//Title: Bin2Hex
//Test by Borland C++ 3.1 and Microsoft Visual C++ 1.0

#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
#include <process.h>    // for exit()
#include <fstream.h>    // for ifstream, ofstream
#define MAXLENTH 80     // 相对于10开头的标准intel HEX 格式文件的行记录长度
=1 + 2 + 4 + 2 + 32*2 + 2 + 2

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

char linebuff[MAXLENTH],bufptr=0;
                     
void main(int argc, char* argv[])   // access command-line arguments
{
   char fnami[15],fnamo[15];        // fnami = input file name etc 
   unsigned int temp;               // Program Counter , Source File line
   unsigned char checksum,tempcrc;
   long  offsetS,offsetD;                   // file offset at writting
   char ch,rt;
   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,".bin");            // add the extension to the filename 
string 

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

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

   source.open(fnami,ios::nocreate); // 源文件必须存在,如果文件不存在亦不予创
建新文件
   if (!source)
   {
      cerr << "Cannot open source file " << fnami << " for input\n";
      exit(-1);
   }
   dest.open(fnamo,ios::out);       // out方式打开目标文件,如果不存在则自动创
建一个新文件。
                                    // 如果文件已经存在,则写入覆盖旧文件。
   if (!dest)
   {
      cerr << "Cannot open destination file " << fnamo << " for output\n";
      exit(-1);
   }
    offsetS = 0;
    offsetD = 0;

    dest.seekp(offsetD);            // 文件写入地址的起始地址移动:hex文件所表
示的地址
    
    while (!source.eof()) {
        checksum=0;
        bufptr=9;
        for(rt=0;rt<16;rt++){

            if(!source.eof()){
                source.get(ch);
                temp = getcode(ch);
                checksum -= ch;
                linebuff[bufptr++]= char(temp>>8);
                linebuff[bufptr++]= char(temp & 0xff);
                offsetS++;
            }
            else break;
        }

// 一个输出行的后处理部分
        if (source.eof()) {
            checksum +=0xff;
            bufptr -= 2;
            rt--;
            offsetS--;
        }
        temp = getcode(rt);
        
        linebuff[0]= :;                           // 引导
        
        linebuff[1]= char(temp>>8);                 // 行内字节数
        linebuff[2]= char(temp & 0xff);
        
        temp = getcode(char((offsetS-rt)>>8));      // 寻址指针(4字节)
        linebuff[3]= char(temp>>8);
        linebuff[4]= char(temp & 0xff);
        temp = getcode(char((offsetS-rt)&0xff));
        linebuff[5]= char(temp>>8);
        linebuff[6]= char(temp & 0xff);
        checksum -= rt + char((offsetS-rt)&0xff)+char((offsetS-rt)>>8);
        linebuff[7]= 0;                           // 属性
        linebuff[8]= 0;
        temp = getcode(checksum);
        linebuff[bufptr++]= char(temp>>8);
        linebuff[bufptr++]= char(temp&0xff);

        linebuff[bufptr++]= 0x0A;
        tempcrc = rt*2 + 9 + 2 + 2;

        for(rt=0;rt<tempcrc;rt++) {
            dest.put(linebuff[rt]);
//          if(rt+1 < tempcrc)printf("%c",linebuff[rt]);
        }
        linebuff[rt-1]=0;
        cout << linebuff;
    offsetD += tempcrc;
    dest.seekp(offsetD);                            // 文件写入地址的起始地址移
动:hex文件所表示的地址

    }
    strcpy(linebuff,":00000001FF\n");

    for(rt=0;rt<12;rt++) {
        dest.put(linebuff[rt]);
//      printf("%c",linebuff[rt]);
    }
    cout << linebuff;
    source.close();                                 // 两个文件都关闭,先关闭源
文件
    dest.close();                                   // 关闭目标文件
    cout << "\n Total " << offsetS-1 << " Bytes Lenth Process OK\n";
}

/*------------------------------- bin码方式向 ascii hex方式转换 ---------------
---------------*/

unsigned int getcode(unsigned char i){
  unsigned char temp;
  unsigned int j;

  temp=i>>4;
  i &= 0xf;                                         // 一字节到可读二字节转换
  if(i<10)i |= 0x30;
  else i = (i-9)|0x40;
  if(temp<10)temp |= 0x30;
  else temp = (temp-9) | 0x40;
    j = int(temp)<<8 | i;
  return j;
}


⌨️ 快捷键说明

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