What's Here?
- Members: 300,442
- Replies: 826,066
- Topics: 137,464
- Snippets: 4,419
- Tutorials: 1,148
- Total Online: 1,520
- Members: 88
- Guests: 1,432
|
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
|
|
|
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
/*
Cat-HEX
written by WolfCoder (2009)
Displays the contents of a file in HEX format and ASCII characters.
*/
/* Defines */
#define DEFAULT_LINES 12
#define WIDE_DIVISOR 16
#define CATHEX_OK 0
#define WRONG_ARG_ERR 1
#define FILE_ERR 2
/* Includes */
#include <stdlib.h>
#include <stdio.h>
/* Globals */
FILE *showfile;
int num_show = DEFAULT_LINES*WIDE_DIVISOR;
int offset = 0;
unsigned char hex[WIDE_DIVISOR];
int num_read = 0;
int so_far_read = 0;
int max_show = 0;
/* Display a line of 8 hex digits */
void display_line()
{
int i;
/* Display */
for(i = 0;i < WIDE_DIVISOR;i++)
{
/* Read data? */
if(i < num_read)
{
/* Small? */
if(hex[i] <= 0xF)
else
printf("%x ",hex [i ]); /* Large */
}
else
{
/* Spacers */
}
}
}
/* Display a line of characters */
void display_ascii_line()
{
int i;
/* Display */
for(i = 0;i < num_read;i++)
{
/* Visible character? */
if(hex[i] >= 0x20 && hex[i] < 0x7F)
else
printf("."); /* Not visible */
}
}
/* Program Entry */
int main(int argn,char **argv)
{
int i;
/* No options */
if(argn == 1)
{
printf("Usage: cathex [filename] [bytes to display] [offset to start from]\n");
return CATHEX_OK;
}
/* Illegal options */
if(argn > 4)
{
fprintf(stderr,"Wrong number of arguments passed.\n");
return WRONG_ARG_ERR;
}
/* Open file */
showfile = fopen(argv[1],"rb");
if(!showfile)
{
fprintf(stderr,"Could not open %s.\n",argv[1]);
return FILE_ERR;
}
/* Set maximum */
if(argn > 2)
max_show = atoi(argv[2]);
/* Set offset */
if(argn > 3)
offset = atoi(argv[3]);
/* Seek */
fseek(showfile,offset,SEEK_SET);
/* Begin reading lines */
while(1)
{
/* Read */
num_read = fread(hex,1,WIDE_DIVISOR,showfile);
/* Show */
display_line();
display_ascii_line();
/* Return */
/* Advance */
so_far_read += num_read;
/* Check for ending */
if(num_read != WIDE_DIVISOR)
break;
if(max_show != 0 && so_far_read >= max_show)
break;
}
/* Everything is alright */
return CATHEX_OK;
}
Copy & Paste
|
|
|
|