| Category: | Utility Script |
| Author/Contact Info | OeufMayo |
| Description: | A nice and simple Hexadecimal viewer module, using the Perl format functions. One cool use in a on-liner: perl -MHexView -e "HexView(\*STDIN)" < some_file you can also use it like this: HexView($any_scalar); HexView(@some_array); HexView(\*FH_REF); |
package HexView;
use strict;
use warnings;
BEGIN {
use Exporter ();
our ($VERSION, @ISA, @EXPORT);
$VERSION = 0.01;
@ISA = qw(Exporter);
@EXPORT = qw(&HexView);
}
sub HexView {
my $data = shift;
# If a filehandle ref was an arg we convert
# it to an array (in a rather ugly fashion)
if (ref($data) eq 'GLOB') {
@_ = <$data>;
undef $data; # clean the 'GLOB(0x123456)'
}
while (@_){$data .= shift}
my ($hex, $char);
foreach (split (//,$data)){
$hex .= sprintf('%02X ', ord($_));
$char .= ord($_) > 13 ? $_ : ".";
}
local $: = ''; # a.k.a $FORMAT_LINE_BREAK_CHARACTERS (and we don't wan
+t that)
my $formathead =<<"HEAD";
format =
+--------------------------------------------------+------------------
++
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | 0123456789ABCDEF
+|
+--------------------------------------------------+------------------
++
HEAD
my $formatline = <<'LINE';
| ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | ^<<<<<<<<<<<<<<<
+|
$hex, $char,
LINE
my $formatend = <<'END';
+--------------------------------------------------+------------------
++
.
END
eval($formathead . $formatline x (int(length($data)/16)+1) . $formaten
+d);
write;
return 1;
}
1;
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HexView
by Anonymous Monk on Nov 17, 2012 at 18:43 UTC |