フォーマットされたバイナリ ファイルを調べる必要がある私たちのことを考えてみてください。これは、MySQL .frm 仕様のように、Intel 以外のバイト順で仕様が 16 進値を示すときに使用するものです。sysread サイズを増やすことで、より大きなチャンクを指定できます。パック/アンパックの長さは、sysread 値の 2 倍でなければなりません。オフセット += [sysread 値に設定]。
#!/usr/bin/perl
BEGIN {
use Getopt::Long;
$reverse=0;
GetOptions ("reverse" => \$reverse);
}
my $f=STDIN;
if (int(@ARGV))
{
open(IN, $ARGV[0]) or die "Failed to open $ARGV[0] for reading.\n$!";
$f=IN;
}
my $count=1;
my $offset=0;
my $after_nl=1;
while (sysread($f,$buf,2))
{
my $hex = unpack('H4', $buf);
$hex=join("",(split(//,$hex))[2,3,0,1]) unless $reverse;
if (($count % 8) == 0)
{
printf "%s\n", $hex;
$after_nl=1;
$offset += 2;
}
else
{
printf "%08x ", $offset if $after_nl;
printf "%s ", $hex;
$offset += 2;
$after_nl=0;
}
$count++;
}