Sunday, December 03, 2006

Binary Banner

Cross posted from

BoingBoing has a story of a store that sells t-shirts with offensive messages spelled out in binary. I think its a nice way to generate email signature or web-page banner where you can really say offensive stuff and get away with it. I wrote the following code in Ruby which takes any word and spits out the binary ASCII representation.

ARGV.each do str
def PrintBin(ch)
mask = 0x80
while mask != 0
print (if (ch & mask) == 0 then "0" else "1" end)
mask >>= 1

str.each_byte do ch
PrintBin ch
print "\n"

My Ruby is completely rusted so it took me some time to get this done. Later I tried writing the same code in C#. It took me over 3 times the LOC it took me to do it up in Ruby. I'm sure a experinced Ruby programmer can make this even more simpler and concise.

C:\MyStuff\Code\Ruby>BinaryBan.rb Krikkit

I have already started using it on my web-site

No comments: