Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

A simple hex dump

by converter (Priest)
on Dec 17, 2001 at 01:56 UTC ( [id://132401]=CUFP: print w/replies, xml ) Need Help??

I don't know how "Cool" this is, but I find this little hex dump subroutine useful from time to time when I'm dealing with data that I'm not familiar with.

Print a simple hex dump, hex on left side, dot-masked ASCII on the right side.

use strict; use warnings; my $string = "non-printable or ill-behaved characters: \x0d \x0a \x00 +\b \a"; # my $string = ''; print hexdump(\$string); sub hexdump { my $str = ref $_[0] ? ${$_[0]} : $_[0]; return "[ZERO-LENGTH STRING]\n" unless length $str; # split input up into 16-byte chunks: my @chunks = $str =~ /([\0-\377]{1,16})/g; # format and print: my @print; for (@chunks) { my $hex = unpack "H*", $_; tr/ -~/./c; # mask non-print chars $hex =~ s/(..)(?!$)/$1 /g; # insert spaces in hex # make sure our hex output has the correct length $hex .= ' ' x ( length($hex) < 48 ? 48 - length($hex) : 0 ); push @print, "$hex $_\n"; } wantarray ? @print : join '', @print; } __END__ prints: 6e 6f 6e 2d 70 72 69 6e 74 61 62 6c 65 20 6f 72 non-printable or 20 69 6c 6c 2d 62 65 68 61 76 65 64 20 63 68 61 ill-behaved cha 72 61 63 74 65 72 73 3a 20 0d 20 0a 20 00 20 08 racters: . . . . 20 07 .

conv

Update:
Changed substitution to tr/// at japhy's suggestion.

For what it's worth, I also have an IBM-style dump that is useful for fixed-width records where columnar alignment is desired. I'll post that one as soon as I find time.

Replies are listed 'Best First'.
Re: A simple hex dump
by kschwab (Vicar) on Dec 17, 2001 at 08:35 UTC
Re: A simple hex dump
by YuckFoo (Abbot) on Dec 17, 2001 at 20:04 UTC
    Yep, more elegant than mine. In the interest of TIMTOWTDI, I'll pile on, here's my brutish code.

    YuckFoo

    #!/usr/bin/perl use strict; if (@ARGV < 1) { print STDERR "\nUsage: $0 filename [bytes]\n\n"; exit; } my ($File, $Bytes) = @ARGV; my ($numread, $totread, $lines); my ($ch, $input, $hex, $text); if (!open(IN, $File)) { print STDERR "\nError opening file: $File\n\n"; exit; } while ($numread = read(IN, $input, 16)) { $totread += $numread; $hex = $text = ''; for $ch (unpack("C16", $input)) { $hex .= sprintf("%2.2x ", $ch); if ($ch >= 0x20 && $ch < 0x7f) { $text .= chr($ch); } else { $text .= '.'; } } substr($hex, 24, 0, ' '); printf(STDOUT "%8.8x %-48.48s %-16.16s\n", $lines * 16, $hex, $text); if (++$lines % 8 == 0) { print "\n"; } if ($Bytes && $totread >= $Bytes) { last; } }
Re: A simple hex dump
by rje (Deacon) on Dec 18, 2001 at 02:50 UTC
    I wrote a hexdump script once for comparing the data dumps
    from character data from the computer game "Diablo II".
    Here it is...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://132401]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found