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 assgave 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
BitemyshinymetalassSo, 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 '' ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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