#!/usr/bin/perl

###########################################################
#
# Take a file (of binary numbers between 0b00000000 and 0b11111111 
# separated by spaces) and a target file.
# write the target using ASCII equivalents of the binary numbers
#
# by Ken Owen
# 1/24/2011
#
###########################################################

if(@ARGV!=2){
	print "\n\nUsage: perl binary_io_program.pl <source> <target>\n\n";
	exit(0);
}

open(X, "$ARGV[0]") || die print "Couldn't open " . $ARGV[0] . "!\n";
open(Y, ">$ARGV[1]") || die print "Couldn't open " . $ARGV[1] . " for writing!\n";

while(<X>){
	@x=split /\s/, $_;
}
my $z;
foreach $x(@x){
	$x=~s/0b//;
	$z=0;
	for($i=0;$i<8;$i++){
		$z+=2**(7-$i)*int(substr($x,$i,1));
	}
	$y=chr($z); 
	print Y $y;
} 
print "All done!\n";