十六进制显示文件

  1. 转自:http://blog.csdn.net/leotnt/article/details/6710843
  2. /* 
  3. Open an file into binary stream 
  4.  
  5. Note that argc counts .\binaryfile.exe -o outputFilePath 
  6. So the argc is 3 
  7. argv[0]==.\binaryfile.exe 
  8. argv[1]== -o 
  9. argv[2]== outputFilePath 
  10. */  
  11.   
  12. #include<iostream>   
  13. #include<fstream>   
  14. #include<iomanip>   
  15. #include<cstring>   
  16. using namespace std;  
  17. int main(int argc,char *argv[])  
  18. {  
  19.     char strPath[256]="";  
  20.     fstream file;  
  21.     char *buffer=NULL;  
  22.     int intHex=0;  
  23.     int length=0;  
  24.     int i,j;  
  25.     unsigned long long addr=0;  
  26.   
  27.     cout<<"Enter file path:"<<endl;  
  28.     cin>>strPath;  
  29.   
  30.     file.open(strPath,fstream::in|fstream::out|fstream::binary);  
  31.   
  32.     if (!file.is_open()){  
  33.         cout<<"Open file error!"<<endl;  
  34.         file.close();  
  35.         return -1;  
  36.     }  
  37.   
  38.     file.seekg(0,fstream::end);  
  39.     length=file.tellg();//get the length of the file  
  40.   
  41.     buffer=new char[length];  
  42.   
  43.     file.seekg(0,fstream::beg);//rewind the pointer  
  44.     file.read(buffer,length);  
  45.     file.close();  
  46.   
  47.     if (argc>1 && !strcmp(argv[1],"-o")){//has an out put argument  
  48.         fstream fout;  
  49.         fout.open(argv[2],fstream::out);  
  50.   
  51.         if (!fout.is_open()){  
  52.             cout<<"Output file error!"<<endl;  
  53.             fout.close();  
  54.             return -1;  
  55.         }  
  56.   
  57.         cout<<"Saving... Please wait..."<<endl;  
  58.   
  59.         fout<<setw(sizeof(long long))<<"Address"<<' ';  
  60.   
  61.         for (i=0;i<16;i++){//print 1-9 A-F markers  
  62.             fout<<' ';  
  63.             if (i<10) fout<<char(i+'0');  
  64.             else fout<<char(i+'A'-10);  
  65.             fout<<' ';  
  66.         }  
  67.   
  68.         fout<<"ASCII"<<endl;  
  69.   
  70.         for (i=0;i<sizeof(long long);i++) fout<<'0';//first address  
  71.         addr+=0x10;  
  72.         fout<<' ';  
  73.   
  74.         cout<<"Processing:00.00%";  
  75.   
  76.         fout<<setiosflags(ios::uppercase);  
  77.   
  78.         for (i=1;i<=length;i++){//read the bytes  
  79.             cout<<"\b\b\b\b\b\b"  
  80.                 <<setw(5)  
  81.                 <<setiosflags(ios::fixed)  
  82.                 <<setprecision(2)  
  83.                 <<double(i)/length*100  
  84.                 <<'\%';  
  85.   
  86.             intHex=buffer[i-1];  
  87.             intHex&=0xFF;//change signed char into hex  
  88.   
  89.             if ((intHex&0x0F)&&(intHex&0xF0)) fout<<hex<<intHex;//high bits and low bits !=0  
  90.             else if (!(intHex&0xF0)) fout<<'0'<<intHex;//high bits are 0  
  91.             else if (!(intHex&0x0F)) fout<<intHex;  
  92.             else fout<<"00";//this byte equals 0  
  93.   
  94.             if (i%16) fout<<' ';  
  95.             else{  
  96.                 fout<<' ';  
  97.   
  98.                 for (j=i-16;j<i;j++){  
  99.                     if (buffer[j]>=0x20&&buffer[j]<=0x7E) fout<<buffer[j];  
  100.                     else fout<<'.';  
  101.                 }  
  102.                 fout<<endl;  
  103.   
  104.                 fout<<setw(8)<<setfill('0')<<addr<<' ';  
  105.                 addr+=0x10;  
  106.             }  
  107.         }  
  108.   
  109.         for (j=0;j<=(16-i%16);j++) fout<<"   ";//last line's ASCII  
  110.   
  111.         for (j=i-i%16;j<length;j++){  
  112.             if (buffer[j]>=0x21&&buffer[j]<=0x7E) fout<<buffer[j];  
  113.             else fout<<'.';  
  114.         }  
  115.         fout<<endl;  
  116.   
  117.         delete[] buffer;  
  118.   
  119.         return 0;  
  120.     }  
  121.     else{  
  122.         cout<<setw(sizeof(long long))<<"Address"<<' ';  
  123.   
  124.         for (i=0;i<16;i++){//print 1-9 A-F markers  
  125.             cout<<' ';  
  126.             if (i<10) cout<<char(i+'0');  
  127.             else cout<<char(i+'A'-10);  
  128.             cout<<' ';  
  129.         }  
  130.   
  131.         cout<<"ASCII"<<endl;  
  132.   
  133.         for (i=0;i<sizeof(long long);i++) cout<<'0';//first address  
  134.         addr+=0x10;  
  135.         cout<<' ';  
  136.   
  137.         cout<<setiosflags(ios::uppercase);  
  138.   
  139.         for (i=1;i<=length;i++){//read the bytes  
  140.             intHex=buffer[i-1];  
  141.             intHex&=0xFF;//change signed char into hex  
  142.   
  143.             if ((intHex&0x0F)&&(intHex&0xF0)) cout<<hex<<intHex;//high bits and low bits !=0  
  144.             else if (!(intHex&0xF0)) cout<<'0'<<intHex;//high bits are 0  
  145.             else if (!(intHex&0x0F)) cout<<intHex;  
  146.             else cout<<"00";//this byte equals 0  
  147.   
  148.             if (i%16) cout<<' ';  
  149.             else{  
  150.                 cout<<' ';  
  151.   
  152.                 for (j=i-16;j<i;j++){  
  153.                     if (buffer[j]>=0x20&&buffer[j]<=0x7E) cout<<buffer[j];  
  154.                     else cout<<'.';  
  155.                 }  
  156.                 cout<<endl;  
  157.   
  158.                 cout<<setw(8)<<setfill('0')<<addr<<' ';  
  159.                 addr+=0x10;  
  160.             }  
  161.         }  
  162.   
  163.         for (j=0;j<=(16-i%16);j++) cout<<"   ";//last line's ASCII  
  164.   
  165.         for (j=i-i%16;j<length;j++){  
  166.             if (buffer[j]>=0x21&&buffer[j]<=0x7E) cout<<buffer[j];  
  167.             else cout<<'.';  
  168.         }  
  169.         cout<<endl;  
  170.   
  171.         delete[] buffer;  
  172.   
  173.         return 0;  
  174.     }  
  175. }  

  176.  

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值