RenHao

Hex_exchange

Jul 11th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. #define PgUp 0x49
  6. #define PgDn 0x51
  7. #define Up 0x48
  8. #define Dn 0x50
  9.  
  10. #define Max_PGline 20
  11.  
  12. void print_hex(char* buf,int *cline,int line,int n,FILE *fp){
  13.    int col;
  14.    n=n>Max_PGline?Max_PGline:n;
  15.    fread(buf, 1, 16*20, fp); // read into memory buffer
  16.    printf("\n\n Hex Dump... \n");
  17.    for(line=0 ; line < n; line++)
  18.    {
  19.      printf("%08x: ",*cline*16);// file position
  20.      for(col=0; col<16; col++)         // 01 01 00 HEX data
  21.        printf("%02X ",buf[line*16+col]);
  22.      for(col=0; col<16; col++)         // ASCII or ....
  23.        if ( isprint(buf[line*16+col]) )   // ctype.h
  24.           printf("%C",buf[line*16+col]);
  25.        else
  26.           printf(".");  
  27.      printf("\n");
  28.      *cline=*cline+1;
  29.    }
  30. }
  31.  
  32. int main(int argc, char *argv[])
  33. {
  34.    char fname[100]; FILE *fp;
  35.    printf("Please enter the file name..:\n");
  36.    scanf("%s", fname);
  37.    if( (fp=fopen(fname,"rb"))== NULL)
  38.    {
  39.        printf("Can't open %s\n", fname);
  40.        system("PAUSE");
  41.        return -1;
  42.    }    
  43.    
  44.    unsigned char hex_buf[20*16];
  45.    int col,c,max_line;
  46.    int current_line = 0,line=0;
  47.    
  48.    fseek(fp,0,SEEK_END);
  49.    c=ftell(fp);
  50.    max_line=c/16+(c%16!=0?1:0);//get max line
  51.    fseek(fp,0,SEEK_SET);
  52.    
  53.    printf("PgUp and PgDn \n");
  54.    print_hex(hex_buf,&current_line,line,20,fp);
  55.    
  56. while(1)
  57. {
  58.    c = getch();  // getch() will return 0 if you press control-function keys
  59.    if(c==27)  break; // ESC
  60.    if (c==224) // PgUp 0x49 or PgDn 0x51
  61.    {
  62.          c = getch();
  63.          if (c == PgUp||c == Up)
  64.          {
  65.                current_line = current_line -(c=(c == PgUp?40:2));
  66.                if (current_line < 0) current_line = 0; //check the line if previous line is out of the file begin
  67.                fseek (fp , current_line*16 , SEEK_SET);
  68.                print_hex(hex_buf,&current_line,line,c-1,fp);
  69.          }
  70.          else if (c == PgDn||c == Dn)
  71.          {
  72.                if(current_line+(c=(c == PgDn?20:1))>max_line) //check the line if next line is out of the file end
  73.                    current_line=max_line-c;                  
  74.                fseek (fp , current_line*16 , SEEK_SET);
  75.                print_hex(hex_buf,&current_line,line,c,fp);
  76.          }
  77.          else  //another key
  78.          {    
  79.                current_line =current_line<Max_PGline?0: current_line - Max_PGline;
  80.                print_hex(hex_buf,&current_line,line,Max_PGline,fp);
  81.          }
  82.     }
  83.     else  //another key
  84.     {    
  85.              current_line =current_line<Max_PGline?0: current_line - Max_PGline;
  86.              print_hex(hex_buf,&current_line,line,Max_PGline,fp);
  87.     }
  88. }
  89.   fclose(fp);
  90.   system("PAUSE"); 
  91.   return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment