Skip to content

Instantly share code, notes, and snippets.

@mkrasowski
Created May 27, 2013 10:42
Show Gist options
  • Save mkrasowski/5656423 to your computer and use it in GitHub Desktop.
Save mkrasowski/5656423 to your computer and use it in GitHub Desktop.
Converts a text file containing hexadecimal representation of a binary file content into a binary file.
use strict;
use warnings;
use 5.014;
use autodie;
do {
say "Usage: hex2bin <input> <output>";
exit(1);
} unless @ARGV == 2;
my ($fin, $fout) = @ARGV;
open( I, "<$fin" );
my $line = <I>;
close(I);
$line =~ s/\s//g;
chomp $line;
my @chars = unpack( "(A2)*", $line);
chomp @chars;
open( O, ">$fout" );
print O chr( hex($_) ) foreach @chars;
close(O);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment