Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/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__

In reply to Dump.pl by hsmyers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-03-19 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found