All the pieces were there in the thread. I simply rearranged them...
#!/usr/bin/perl -wT
use strict;
my $bin = ascii2bin('Rolf');
my $ascii = bin2ascii($bin);
print "bin => $bin\n";
print "ascii => $ascii\n";
sub ascii2bin {
my $ascii = shift;
my $bin = join '', map {sprintf "%08b", ord($_)} split //, $ascii;
return $bin;
}
sub bin2ascii {
my $bin = shift;
my $ascii = join '', map {chr(oct"0b$_")} $bin =~ /(\d{8})/g;
return $ascii;
}
=OUTPUT
bin => 01010010011011110110110001100110
ascii => Rolf
Or, as one-liners.....
perl -le 'print join"",map{sprintf"%08b",ord($_)}split//,pop' Rolf
perl -le 'print join"",map{chr(oct"0b$_")}pop=~/(\d{8})/g' 01010010011
+011110110110001100110
-Blake
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.