School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 300,442 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,520 people online right now. Registration is fast and FREE... Join Now!





HEX File Data Display

Displays bytes in a file from an optional offset to an optional maximum value of bytes to show to the terminal. I made this for Linux but it will work in Windows.

Submitted By: WolfCoder
Actions:
Rating:
Views: 453

Language: C

Last Modified: July 1, 2009
Instructions: Just compile and run. Program has it's own instructions inside for usage. For Windows, you *might* have to change the file operations to use their _s forms (ex: fopen_s) for it to compile in MSVS.

Snippet


  1. /*
  2.         Cat-HEX
  3.         written by WolfCoder (2009)
  4.         Displays the contents of a file in HEX format and ASCII characters.
  5. */
  6.  
  7. /* Defines */
  8. #define DEFAULT_LINES 12
  9. #define WIDE_DIVISOR 16
  10.  
  11. #define CATHEX_OK 0
  12. #define WRONG_ARG_ERR 1
  13. #define FILE_ERR 2
  14.  
  15. /* Includes */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18.  
  19. /* Globals */
  20. FILE *showfile;
  21. int num_show = DEFAULT_LINES*WIDE_DIVISOR;
  22. int offset = 0;
  23. unsigned char hex[WIDE_DIVISOR];
  24. int num_read = 0;
  25. int so_far_read = 0;
  26. int max_show = 0;
  27.  
  28. /* Display a line of 8 hex digits */
  29. void display_line()
  30. {
  31.         int i;
  32.         /* Display */
  33.         for(i = 0;i < WIDE_DIVISOR;i++)
  34.         {
  35.                 /* Read data? */
  36.                 if(i < num_read)
  37.                 {
  38.                         /* Small? */
  39.                         if(hex[i] <= 0xF)
  40.                                 printf("0%x ",hex[i]);
  41.                         else
  42.                                 printf("%x ",hex[i]); /* Large */
  43.                 }
  44.                 else
  45.                 {
  46.                         /* Spacers */
  47.                         printf("   ");
  48.                 }
  49.         }
  50. }
  51.  
  52. /* Display a line of characters */
  53. void display_ascii_line()
  54. {
  55.         int i;
  56.         /* Display */
  57.         for(i = 0;i < num_read;i++)
  58.         {
  59.                 /* Visible character? */
  60.                 if(hex[i] >= 0x20 && hex[i] < 0x7F)
  61.                         printf("%c",(char)hex[i]);
  62.                 else
  63.                         printf("."); /* Not visible */
  64.         }
  65. }
  66.  
  67. /* Program Entry */
  68. int main(int argn,char **argv)
  69. {
  70.         int i;
  71.         /* No options */
  72.         if(argn == 1)
  73.         {
  74.                 printf("Usage: cathex [filename] [bytes to display] [offset to start from]\n");
  75.                 return CATHEX_OK;
  76.         }
  77.         /* Illegal options */
  78.         if(argn > 4)
  79.         {
  80.                 fprintf(stderr,"Wrong number of arguments passed.\n");
  81.                 return WRONG_ARG_ERR;
  82.         }
  83.         /* Open file */
  84.         showfile = fopen(argv[1],"rb");
  85.         if(!showfile)
  86.         {
  87.                 fprintf(stderr,"Could not open %s.\n",argv[1]);
  88.                 return FILE_ERR;
  89.         }
  90.         /* Set maximum */
  91.         if(argn > 2)
  92.                 max_show = atoi(argv[2]);
  93.         /* Set offset */
  94.         if(argn > 3)
  95.                 offset = atoi(argv[3]);
  96.         /* Seek */
  97.         fseek(showfile,offset,SEEK_SET);
  98.         /* Begin reading lines */
  99.         while(1)
  100.         {
  101.                 /* Read */
  102.                 num_read = fread(hex,1,WIDE_DIVISOR,showfile);
  103.                 /* Show */
  104.                 display_line();
  105.                 printf("\t");
  106.                 display_ascii_line();
  107.                 /* Return */
  108.                 printf("\n");
  109.                 /* Advance */
  110.                 so_far_read += num_read;
  111.                 /* Check for ending */
  112.                 if(num_read != WIDE_DIVISOR)
  113.                         break;
  114.                 if(max_show != 0 && so_far_read >= max_show)
  115.                         break;
  116.         }
  117.         /* Everything is alright */
  118.         return CATHEX_OK;
  119. }
  120.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month