Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Dump.pl

by hsmyers (Canon)
on Oct 03, 2001 at 06:42 UTC ( #116337=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info hsmyers@sdragons.com
Description: Nothing fancy, just a straight forward binary dump of a file in a formatted display to either STDOUT or filename. Particularly handy to compare what you think is in the file with what is actually in the file!

hsm

#!/usr/bin/perl
# dump.pl -- create hex dump of input file.
use strict;
use warnings;
use diagnostics;
use Getopt::Std;
use Config;

my $filename = $ARGV[0];
my $output   = $ARGV[1];
my $buffer;
my $address = 0;
my $old_fh;

$0 =~ s|.*[/\\]||;
my $usage = <<EOT;
Usage:  $0 [-h]
   or:  $0 file_in [file_out]
EOT

my %OPT = ();
warn($usage), exit(0) if !getopts( 'h', \%OPT ) or $OPT{'h'};
if ($output) {
    open( OUT, "> $output" ) || die "Couldn't open $output for output:
+ $!\n";
    $old_fh = select(OUT);
}
die "No filename specified\n" unless ($filename);
open( FILE, $filename ) || die "Couldn't open $filename: $!\n";
binmode FILE;

while ( read( FILE, $buffer, 16 ) ) {
    printf( "%08x: %s | %-16s\n", $address, hexstr($buffer),
      makesafe($buffer) );
    $address += 16;
}
close(FILE);

if ($output) {
    select($old_fh) if ($output);
    close(OUT);
}

sub makesafe {
    my @list = unpack( 'C*', $_[0] );
    foreach (@list) {
        if ( $_ < 32 || $_ > 126 ) {
            $_ = 46;
        }
    }
    my $result = pack( 'C*', @list );
    return $result;
}

sub hexstr {
    my @list = unpack( 'H32', $_[0] );
    my $result = sprintf( "%-32s", $list[0] );
    my $expanded;

    while ( $result =~ /(..)/g ) {
        $expanded .= $1 . ' ';
    }
    return $expanded;
}

=pod
=head1 NAME

dump - output a formated hex dump of a selected file

=head1 SYNOPSIS

B<dump> B<-h>

B<dump> filename

B<dump> file_in file_out

=head1 DESCRIPTION

This utility converts a file into the formated hexidecimal representat
+ion of itself.
Output is to either STDOUT or to a specified file.

=head1 EXAMPLES

    C:\dump dump.pl
    creates:
        00000000: 23 21 2f 75 73 72 2f 62 69 6e 2f 70 65 72 6c 20  | #
+!/usr/bin/perl 
        00000010: 2d 77 0a 23 20 64 75 6d 70 2e 70 6c 20 2d 2d 20  | -
+w.# dump.pl -- 
        00000020: 63 72 65 61 74 65 20 68 65 78 20 64 75 6d 70 20  | c
+reate hex dump 
        00000030: 6f 66 20 69 6e 70 75 74 20 66 69 6c 65 2e 0a 0a  | o
+f input file...
        00000040: 75 73 65 20 73 74 72 69 63 74 3b 0a 75 73 65 20  | u
+se strict;.use 
        00000050: 77 61 72 6e 69 6e 67 73 3b 0a 0a 6d 79 20 24 66  | w
+arnings;..my $f
        .
        .
        .
        00000390: 64 65 64 20 2e 3d 20 24 31 2e 27 20 27 3b 0a 09  | d
+ed .= $1.' ';..
        000003a0: 7d 0a 09 72 65 74 75 72 6e 20 24 65 78 70 61 6e  | }
+..return $expan
        000003b0: 64 65 64 3b 0a 7d                                | d
+ed;.}          
    

    C:\dump dump.pl out.txt
    creates: the file out.txt as above...

    C:\dump -h
    shows: command line usage

=head1 SEE ALSO

coredump, divination, entrails

=head1 BUGS

    C:\dump filename > outfile
    won't work if this is made a dos batch file...

=head1 AUTHOR

Hugh S. Myers
hsmyers@sdragons.com

=cut
__END__

Comment on Dump.pl
Download Code
Re: Dump.pl
by premchai21 (Curate) on Oct 04, 2001 at 04:40 UTC

    Just thought you might want to know that the two big Emacsen (FSFEmacs and XEmacs) come with hexl, which does exactly this. hexl is also used for hexl-mode, which allows one to edit files in this form.

    Update in response to hsmyers's reply: You don't need to use Emacs itself to use hexl. It's a separate executable that takes ARGV and hexlifies it to STDOUT.

      Knew that, but this is handier— at least to the way I work. Love emacs, but see no reason to live in it!

      hsm

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://116337]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-07-13 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls