Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define PgUp 0x49
- #define PgDn 0x51
- #define Up 0x48
- #define Dn 0x50
- #define Max_PGline 20
- void print_hex(char* buf,int *cline,int line,int n,FILE *fp){
- int col;
- n=n>Max_PGline?Max_PGline:n;
- fread(buf, 1, 16*20, fp); // read into memory buffer
- printf("\n\n Hex Dump... \n");
- for(line=0 ; line < n; line++)
- {
- printf("%08x: ",*cline*16);// file position
- for(col=0; col<16; col++) // 01 01 00 HEX data
- printf("%02X ",buf[line*16+col]);
- for(col=0; col<16; col++) // ASCII or ....
- if ( isprint(buf[line*16+col]) ) // ctype.h
- printf("%C",buf[line*16+col]);
- else
- printf(".");
- printf("\n");
- *cline=*cline+1;
- }
- }
- int main(int argc, char *argv[])
- {
- char fname[100]; FILE *fp;
- printf("Please enter the file name..:\n");
- scanf("%s", fname);
- if( (fp=fopen(fname,"rb"))== NULL)
- {
- printf("Can't open %s\n", fname);
- system("PAUSE");
- return -1;
- }
- unsigned char hex_buf[20*16];
- int col,c,max_line;
- int current_line = 0,line=0;
- fseek(fp,0,SEEK_END);
- c=ftell(fp);
- max_line=c/16+(c%16!=0?1:0);//get max line
- fseek(fp,0,SEEK_SET);
- printf("PgUp and PgDn \n");
- print_hex(hex_buf,¤t_line,line,20,fp);
- while(1)
- {
- c = getch(); // getch() will return 0 if you press control-function keys
- if(c==27) break; // ESC
- if (c==224) // PgUp 0x49 or PgDn 0x51
- {
- c = getch();
- if (c == PgUp||c == Up)
- {
- current_line = current_line -(c=(c == PgUp?40:2));
- if (current_line < 0) current_line = 0; //check the line if previous line is out of the file begin
- fseek (fp , current_line*16 , SEEK_SET);
- print_hex(hex_buf,¤t_line,line,c-1,fp);
- }
- else if (c == PgDn||c == Dn)
- {
- if(current_line+(c=(c == PgDn?20:1))>max_line) //check the line if next line is out of the file end
- current_line=max_line-c;
- fseek (fp , current_line*16 , SEEK_SET);
- print_hex(hex_buf,¤t_line,line,c,fp);
- }
- else //another key
- {
- current_line =current_line<Max_PGline?0: current_line - Max_PGline;
- print_hex(hex_buf,¤t_line,line,Max_PGline,fp);
- }
- }
- else //another key
- {
- current_line =current_line<Max_PGline?0: current_line - Max_PGline;
- print_hex(hex_buf,¤t_line,line,Max_PGline,fp);
- }
- }
- fclose(fp);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment