Decoding Fake Binary Messages

(Comments)

Sometimes I see posts on Facebook featuring a series of zeroes and ones mimicking a binary stream:

Sample fake binary message from Facebook

I call this «fake binary» because real binary is not made of characters. Characters are made of binary bits, though.

No big deal but, just for the fun of it, I wrote a C one-liner for decoding such messages

main(){int c,b=0,m=8;while((c=getchar())>=0){c-=48;if((c&1)==c)b=((b<<1)|c),m--;if(!m)putchar(b),m=8;}}

How fun is to write silly stuff! I ended up creating an encoder (now I had a CODEC!) and a GitHub repo for them.

Enough of playing, now back to work...

Update: more lazy time and made a shorter and slightly smarter version:

main(){int c,b=0,m=8;while((c=getchar())>=0){b=((b<<1)|c&1);if(!--m)putchar(b),b=0,m=8;}}

... and even shorter:

main(){int c,b=0,m=8;while((c=getchar())>=0)b<<=1,b|=c&1,!--m&&putchar(b&127,m=8);}

This is fun!

Currently unrated

Comments