0: <code>
1: # This is a simple script that dumps a hexadecimal
2: # representation of a file
3: # It was origginally written in C, so it suffers from some
4: # C syntax (mostly `for' loops could probably be redone
5: # in a more `perlish' way and the use of ternary operator
6: # ?: is quite ugly). It also overuses printf's where a more
7: # elegant solution with `unpack' could be found.
8: # Never-the-less, it did the job that I needed to do, so
9: # here it is for all to see (and maybe even use).
10:
11: use strict;
12: use warnings;
13: $#ARGV == 0 or die 'Usage: hexdump.pl file';
14: open my $inpt, "<$ARGV[0]" or die "Can't open $ARGV[0]";
15: binmode $inpt; # for DOS and the rest...
16: for(my $line_no = 0; my $i = read $inpt, $_, 16; $line_no++)
17: {
18: push my @a, split //; # put things onto char array
19: printf "%05x ", $line_no; # print line number
20: # print hex codes
21: printf "%02x ", ord() & 0xff foreach @a;
22: # pad last line if neccessary
23: for(my $j = $#a; $j < 15; $j++) { print ' '; }
24: # if a character is a control character, print a dot,
25: # else print normal ascii char
26: (ord() < 32 or ord() == 127) ? print "." : print foreach @a;
27: print "\n";
28: }
29: close $inpt;
30: </code> In reply to simple hexdump by moxliukas
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |