from_hex.pl 534 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/perl
  2. # Convert HEX to binary.
  3. use 5.020;
  4. use strict;
  5. use warnings;
  6. use Getopt::Long qw(GetOptions);
  7. my $low_nybble = 0;
  8. GetOptions("l|low!" => \$low_nybble)
  9. or die "Error in arguments";
  10. my $hex_str = '';
  11. while (<>) {
  12. # Make sure the line starts with an hexadecimal
  13. if (/^[[:xdigit:]]/) {
  14. # Collect all hexadecimal strings from the line
  15. while (/([[:xdigit:]]+)/g) {
  16. $hex_str .= $1;
  17. }
  18. }
  19. }
  20. binmode(STDOUT, ':raw');
  21. print pack(($low_nybble ? "h*" : "H*"), $hex_str);