# dec.pl

use strict;
use utf8;

my $space0 = ' ';
my $space1 = chr 0x3000;

my @textData = split '', readUTF8File("encoded.txt");

my $binaryData = "";

foreach (@textData) {
	my $char = $_;
	
	if ($char eq $space0) { $binaryData .= '0'; }
	if ($char eq $space1) { $binaryData .= '1'; }
}


# remove leading and trailing zeroes
$binaryData =~ s/^0*1 | 10*$//xg;

foreach (unpack "a8" x (length($binaryData) / 8), $binaryData) {
	my $byte = pack 'B8', $_;

	print $byte;
}


sub writeBinaryFile {
	my $filename = shift;
	my $bytes = shift;
	
	open my $fh, ">$filename" or die "Failed to open file: $filename";
	print $fh $bytes;
	close $fh;
}

sub readUTF8File {
	my $filename = shift;

	open my $fh, '<:encoding(UTF-8)', $filename or die "Failed to open text file: $filename";
	my $chars = join '', <$fh>;
	close $fh;
	
	return $chars;
}