现在的位置: 首页 > 综合 > 正文

文件转为十六进制文本

2013年04月06日 ⁄ 综合 ⁄ 共 1253字 ⁄ 字号 评论关闭

//========================================================================
//TITLE:
//    文件转为十六进制文本
//AUTHOR:
//    norains
//DATE:
//    Saturday  29-April-2006
//========================================================================
    在做直接写屏时,需要将32*32位图数据转为数组,为求方便,信手写了如下的转换代码.比较简单,没有做过多解释
   
#include "iostream"
#include "stdio.h"
#include "fstream"

using namespace std;

int main()
{
 
 char szInFileName[80];  //读取的文件名
 char szOutFileName[80]; //数据输出的文件名
  
 cout<<"Please type the file name for chang:"<<endl;
 cin>>szInFileName;   //输入要读取的文件路径
 
 cout<<"Please input the file name for output:"<<endl;
 cin>>szOutFileName; 

    FILE *ifp;   
    if((ifp=fopen(szInFileName,"r"))==NULL)
    {
         cout<<"Open file to read failed"<<endl;
         goto ERRO;
    }

 FILE *ofp;
    if((ofp=fopen(szOutFileName,"w"))==NULL)
    {
         cout<<"Open file to write failed"<<endl;
         goto ERRO;
    }

 

 
 int iBuf,iColCount,iRowCount;
 iColCount=iRowCount=0;
   
 while(feof(ifp)==0)
    {
  iBuf=fgetc(ifp);

  if(iBuf<=9)
  {
    fprintf(ofp,"0x0%x,",iBuf);  //%x参数指以十六进制形式输出到文件中
  }
  else
  {
   fprintf(ofp,"0x%x,",iBuf);
  }
  if(++iColCount==64)
  {
   iColCount=0;
   fprintf(ofp,"/n");

   if(++iRowCount==8)
   {
    iRowCount=0;
    fprintf(ofp,"/n");    
   }
  }
  
    }

 

 
ERRO:
 fclose(ifp);
 fclose(ofp);
 //system("PAUSE"); //vc会自动加上此句.
 return 0;
}

抱歉!评论已关闭.