Re: Hex display under telnet?

From: Jean-Marc Monnez (monnez.jean-marc@AGORA.MSA.FR)
Date: Wed Sep 18 2002 - 05:50:14 EDT


Unix provides 'od -cx filename' but I find the output is not friendly to read.

Here after a C source I wrote to get an "old PCTools"-like output (I think it
could be rewritten somewhat simpler in Perl). Just join the broken lines to
compile. Usage with -h option. Can read stdin if no filenames given :
-----------------------------------------------------------------------------------

/*
   Dump hexa + ascii (if printable) more friendly than od -xc output
   JMM
*/
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>

#define MAX_CHARS_PER_OUTPUTLINE 64

int chars_per_outputline=16;

main(argc,argv)
int argc;
char **argv;
{
    int opt;
    extern int optind;
    extern char *optarg;

    /* Options treatment */
    while ((opt=getopt(argc,argv,"w:")) != EOF)
    {
        switch (opt)
        {
            case 'w': /* width -> number of decoded chars per output line */
                chars_per_outputline=((MAX_CHARS_PER_OUTPUTLINE)<(atoi(optarg))
? (MAX_CHARS_PER_OUTPUTLINE) : (atoi(optarg)));
                break;
            case '?': /* wrong option */
                fprintf(stderr,"Usage: %s [-w linewidth] [file1 [file2 [...]]]\n

",argv[0]);
                exit(2);
        } /* switch */
    } /* while */

    if (optind == argc)
        odxa_output(stdin);
    else
    {
        FILE *fd;
        for ( ; optind<argc ; optind++)
        {
            if (access(argv[optind],R_OK) == 0)
            {
                if ((fd = fopen(argv[optind],"r")) != NULL)
                {
                    odxa_output(fd);
                    fclose(fd);
                }
                else
                    fprintf(stderr,"Unable to open %s for reading\n",argv[optind

]);
            }
            else
                fprintf(stderr,"Cannot access %s\n",argv[optind]);
        } /* for */
    } /* if */
}/* main */

/*
Formatted output of dumped filedesc content
*/
odxa_output(filedesc)
FILE *filedesc;
{
    register c;
    register long i;
    char ascii[MAX_CHARS_PER_OUTPUTLINE+1], hexa[(3*MAX_CHARS_PER_OUTPUTLINE)+1]

;

    for (i=0;;i++)
    {
        sprintf(&ascii[i%chars_per_outputline], "%c", isprint(c=fgetc(filedesc))

?c:'.');
        sprintf(&hexa[(i%chars_per_outputline)*3], "%02x ", c);
        if (i%chars_per_outputline == chars_per_outputline-1 || c == EOF )
        {
            if (c == EOF)
            {
                ascii[i%chars_per_outputline]='\0';
                hexa[(i%chars_per_outputline)*3]='\0';
            }
            printf("%07ld %-*s| %-*s |\n", i-(i%chars_per_outputline), chars_pe

r_outputline*3, hexa, chars_per_outputline, ascii);
        }
        if (feof(filedesc)) break;
    } /* for */
} /* odxa_output */
------------------------------------------------------------------------------------------

Regards

-- JMM

Robert Bardos wrote :

> How do I display the contents of a (small) file in a "hex dump like" format
> under telnet?
> (I want to verify a custom built 256 byte EBCDIC to ASCII translate table.)
>
> Many thanks in advance
> Robert
>
> Sicherheitsmeldung: Diese Mitteilung ist ungeschuetzt und koennte waehrend
> der Uebermittlung von Dritten eingesehen und veraendert werden. Der Absender
> uebernimmt deshalb fuer den Inhalt keine Haftung. Sollten Sie diese
> Mitteilung irrtuemlich erhalten haben, bitten wir Sie, den Absender zu
> informieren und die Mitteilung in Ihrem System zu loeschen.
> Security Message: The information transmitted is unsecured and could be
> reviewed or changed by an unauthorized third party. The sender therefore
> does not accept any liability for its content. If you received this in
> error, please contact the sender and delete the material from any computer.



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:16:12 EDT