Perl hack: ascii-binary

Tim Chambers timc+perl at divide.net
Fri Apr 2 14:58:34 CST 2004


Snipped from irc://irc.community.tummy.com/hackingsociety today. Quote of
the day: "once again, irc to the rescue :)".

s 12:47
Perl hack
<BorD> how do you convert from binary to ascii on the command line?
<MC|w> convert what?
<swtaylor> BorD: theres a 'calc' or something like that which is a
calculator... or you courl perl -e"print %something"
<BorD>  man calc
<BorD> No manual entry for calc
<MC|w>
http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=%2Bbinary+%2Bascii+%2Bconvert&btnG=Google+Search
<swtaylor> man bc
<BorD> i did man bc, see octal, hex, binary, decimal
<BorD> and i already looked at google.  they have nice little applets that
will do it.  i wanted to do it on the command line
<swtaylor> (theres also dc... man -k calc)
<swtaylor> you can use bc or something to convert binary to octal and pipe
that into an echo -e "\`script to spit out octal`"
<swtaylor> though you could probably do it directly by evaluationg the
proper perl string
<tbc> BorD: gimme the problem, I'll give you the perl snippet.
<BorD>
0111010001101000011010010111001100100000011010010111001100100000011000010010
000001110100011001010111001101110100
<BorD> i want to convert that to ascii
<BorD> on the command line
<tbc> I thought you wanted packed binary, like ^A ^@ stuff. Yours is a snap.
Hold on...
<BorD> no, but thats what gave me the idea.
<BorD> there are tools on the web to convert it..
<swtaylor> lol. i love thinkgeek
<BorD> but i wondered where the heck the tool was to just do it easily
<BorD> both ascii to binary and binary to ascii
<tbc> BorD: is that msb or lsb order? Can I assume the binary string can be
split into byte-chunks for interpretation?
<BorD> heck if i know.
<MC|w> yeah, it's like my tie that says "I Hate Ties"
<tbc> I'll try all the decoding techniques to see what sense comes of it.
<MC|w> those are 8bit-at-a-time binary reps of an ascii character
<tbc> cracked it
<tbc> $ perl -e '$s=$ARGV[0];$l=length $s;@a=pack "B$l",$s;print "@a\n"'
0111010001101000011010010111001100100000011010010111001100100000011000010010
000001110100011001010111001101110100
<tbc> this is a test
<tbc> $
<tbc> here's the reverse, FWIW
<tbc> $ perl -e '$s=$ARGV[0];$l=(length $s)*8;@a=unpack "B$l",$s;print
"@a\n"' "this is a test"
<tbc>
0111010001101000011010010111001100100000011010010111001100100000011000010010
000001110100011001010111001101110100
<tbc> $
<tbc> That was fun!
<BorD> once again, irc to the rescue :)
<BorD> could i < filename and read it in from a file?
<tbc> $ echo -e "this is the first line\nthis is the second line" >
/tmp/ascii
<tbc> $ perl -ne '$l=(length $_)*8;@a=unpack "B$l",$_;print "@a\n"'
/tmp/ascii|tee /tmp/bits
<tbc>
0111010001101000011010010111001100100000011010010111001100100000011101000110
1000011001010010000001100110011010010111001001110011011101000010000001101100
01101001011011100110010100001010
<tbc>
0111010001101000011010010111001100100000011010010111001100100000011101000110
1000011001010010000001110011011001010110001101101111011011100110010000100000
0110110001101001011011100110010100001010
<tbc> $ perl -ne 'chomp;$l=length $_;next if(!$l);@a=pack "B$l",$_;print
"@a"' /tmp/bits
<tbc> this is the first line
<tbc> this is the second line
<tbc> $
<tbc> tada :-)
<BorD> someone should hire you :)
* tbc smiles
<BorD> i love it when i have an itch and someone else scratches it :)
<tbc> Hey, don't think this means we're getting married or anything. :-)
<BorD> hahaha
f 13:38




More information about the Pikes-peak-pm mailing list