#!/usr/bin/perl # hex2bin.pl - convert hexadecimal data to binary data or inverse if (grep(/^-.*i/, @ARGV)) { $inverse = 1; } if (grep(/^-.*n/, @ARGV)) { $through_newline = 1; } #if ($0 eq 'hex2bin.pl') { if (!$inverse) { while (read(STDIN, $data, 1)) { if ($data =~ /[0-9a-fA-F]/) { if ($upper ne '') { printf('%c', hex($upper.$data)); $upper = ''; } else { $upper = $data; } } elsif($through_newline && $data eq "\n") { print $data; } } } else { while (read(STDIN, $data, 1)) { if($through_newline && $data eq "\n") { print $data; next; } printf('%02x', ord $data); } }