So, I was looking for a short and elegant binary awk converter. Not satisfied saw this as a challenge, so you are here. Slightly optimized for size, so I posted a readable version below.
The end of printf indicates how large the numbers should be. In this case, 8 bits.
Is this bad code? Hmm, yes ... this is awk :-) Of course, it does not work with very large numbers.
Awk code length 67 characters:
awk '{r="";a=$1;while(a){r=((a%2)?"1":"0")r;a=int(a/2)}printf"%08d\n",r}'
Edit: 55 characters awk code
awk '{r="";a=$1;while(a){r=a%2r;a=int(a/2)}printf"%08d\n",r}'
Readable Version:
awk '{r=""
And asked one liner with bin and hex
awk '{r="";a=$1;while(a){r=a%2r;a=int(a/2)}printf"%08d 0x%02x\n",r,$1}'
source share