Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2015/05/19

01010100 01101000 01100001 01110100 00100111 01110011 00100000 01110111 01100101 01101001 01110010 01100100 00101110 00101110 00101110

Slight language warning.

The PaleoFuture blog on Gizmodo had a review of Tomorrowland from a Robot from the Future today, and of course it's in binary. I saw that shortly after coming in, and I thought "Hey, that'd be an interesting exercise to use to prime my programming pump, get my head in the right mindset."

So, I wrote code to decode it. I read perldoc -f pack and thought "Nope", so I found Data::Translate, which I now know is a very simple wrapper.

Once I wrote that, I decided the thing to do was to write b2a.pl and a2p.pl, because why not? But I found that this
a2b.pl Bite my shiny metal ass
gave me this
01000010 01101001 01110100 01100101 00100000 01101101 01111001 00100000 01110011 01101000 01101001 01101110 01111001 00100000 01101101 01100101 01110100 01100001 01101100 00100000 01100001 01110011 01110011
but this
b2a.pl 01000010 01101001 01110100 01100101 00100000 01101101 01111001 00100000 01110011 01101000 01101001 01101110 01111001 00100000 01101101 01100101 01110100 01100001 01101100 00100000 01100001 01110011 01110011
gave me this
Bitemyshinymetalass
So, what went wrong? Where did the spaces go?

This will take some time with me trying to grok pack, which is so unrelated to my daily responsibilities that I installed Data::Translate simply so I wouldn't have to learn a thing, but it seems that, if you pass pack the space character in binary (00100000), it returns an empty string. This is a somewhat strange thing, but once you understand the behavior, you can react to it.

#!/usr/bin/env perl
use feature qw'say' ;
use strict ;
use warnings ;
use Data::Translate ;
my $data = new Data::Translate;
# allows a2b.pl foobarblee or echo foobarblee | a2b.pl
my @start ;
if ( scalar @ARGV ) {
@start = @ARGV ;
}
else {
while (<STDIN>) { push @start , $_ }
}
say join ' ' ,
map { ( $data->a2b( $_ ) )[ 1 ] }
split m{} ,
join ' ' , @start ;
#space between bytes because that's what the Robot did
view raw a2b.pl hosted with ❤ by GitHub
#!/usr/bin/env perl
use feature qw'say' ;
use strict ;
use warnings ;
use Data::Translate ;
my $data = new Data::Translate;
my @start ;
if ( scalar @ARGV ) {
@start = @ARGV ;
}
else {
while (<STDIN>) { push @start , $_ }
}
my $start = join ' ' , @start ; # binary octets separated by space chars
for my $l ( split m{\s+} , $start ) {
my ( undef , $o ) = $data->b2a( $l ) ;
# print $o ; # the broken behavior
print $o eq '' ? ' ' : $o ; # knows, if you get '' , you want ' '
}
say '' ;
view raw b2a.pl hosted with ❤ by GitHub
# https://metacpan.org/source/DAVIEIRA/Data_Translate-0.3/Translate.pm
sub b2a {
shift;
local ($binstr)= @_;
if ($binstr=~/^[01]+$/) {
$as=unpack("A*", pack("B*", $binstr));
return 1,$as;
} else {
return -1,0;
}
}

(The catchphrase for Bender seemed the most appropriate text to use.)


No comments:

Post a Comment