以前、ファイルの中身を16進ダンプするperlスクリプトを作成した。
akrad.hatenablog.com
上記に対して、今回作成したperlスクリプト makeBinFromHex.pl は、この逆を行うものである。つまり、16進ダンプ文字列からバイナリ文字列を作成する。例えば、入力ファイルに「616263」と書かれていたら、出力ファイルには「abc」が出る。
使い方は簡単で「perl makeBinFromHex.pl "ファイル"」と実行すると、ファイルの中身をバイナリ文字列にしたもの (packしたもの) を "packed_ファイル" に出力する。
makeBinFromHex.pl
use strict; use warnings; my $inFile = $ARGV[0]; if (!-f $inFile) { die "There is not $inFile file..."; } my $outFile = "packed_" . $inFile; open(my $inFh, "< $inFile") or die("Error :$!"); open(my $outFh, "> $outFile") or die("Error :$!"); while (my $line = <$inFh>) { chomp($line); my $binData = pack("H*", $line); print($outFh $binData); } close($inFh); close($outFh);