[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Odd characters



Jeff Licquia said:
[simple hex dump script]

Don't forget that the first edition of Programming Perl also had a
little hex dump script...  If you go to ftp.ora.com and look in
/pub/examples/perl/programming_perl (I think), you can find all the
example programs.  The one in question is ch6/xdump.  I've attached it
for anybody who is interested.

Steve
-- 
steve@silug.org           | Linux Users of Central Illinois
(217)698-1694             | Meetings the 4th Tuesday of every month
Steven Pritchard          | http://www.luci.org/ for more info
#!/usr/bin/perl

# Usage: xdump [file]

# Use the file they specified, if specified

open(STDIN,$ARGV[0]) || die "Can't open $ARGV[0]: $!\n"
    if $ARGV[0];

# Do it optimally as long as we can read 16 bytes at a time.

while (($len = read(STDIN,$data,16)) == 16) {
    @array = unpack('N4', $data);
    $data =~ tr/\0-\37\177-\377/./;
    printf "%8.8lx    %8.8lx %8.8lx %8.8lx %8.8lx    %s\n",
	$offset, @array, $data;
    $offset += 16;
}

# Now finish up the end a byte at a time.

if ($len) {
    @array = unpack('C*', $data);
    $data =~ y/\0-\37\177-\377/./;
    for (@array) {
	$_ = sprintf('%2.2x',$_);
    }
    push(@array, '  ') while $len++ < 16;
    $data =~ s/[^ -~]/./g;
    printf "%8.8lx    ", $offset;
    printf "%s%s%s%s %s%s%s%s %s%s%s%s %s%s%s%s    %s\n",
	@array, $data;
}