awk binary string to ascii string converter

There are probably better ways to this with lshift and rshift functions built into awk, but this is functional.

{
  while (++i <= NF) {
    total = 0
    j = 0
    while (++j <= length($i)) {
      if (j == 1) {
        total += int(substr($i,j,1)) * 128
      }
      else if (j == 2) {
        total += int(substr($i,j,1)) * 64
      }
      else if (j == 3) {
        total += int(substr($i,j,1)) * 32
      }
      else if (j == 4) {
        total += int(substr($i,j,1)) * 16
      }
      else if (j == 5) {
        total += int(substr($i,j,1)) * 8
      }
      else if (j == 6) {
        total += int(substr($i,j,1)) * 4
      }
      else if (j == 7) {
        total += int(substr($i,j,1)) * 2
      }
      else if (j == 8) {
        total += int(substr($i,j,1)) * 1
      }
    }
    printf("%c",total)
  }
  printf("\n")
}

Place what is above in a file name translateBinary.awk, or whatever you like. You can then convert a string with something similar to what is below.

[root@expressdb1 ~]# echo "01001100 01101001 01101011 01100101 00100000 00100110 00100000 01100011 01101111 01101101 01101101 01100101 01101110 01110100 00100000 00100010 01000001 01000011 00110011 00100010 00100000 01100110 01101111 01110010 00100000 01100001 00100000 01100011 01101000 01100001 01101110 01100011 01100101 00100000 01110100 01101111 00100000 01110111 01101001 01101110 00100000 01100001 00100000 01000110 01010010 01000101 01000101 00100000 01100100 01101111 01110111 01101110 01101100 01101111 01100001 01100100 00100000 01101111 01100110 00100000 01000001 01110011 01110011 01100001 01110011 01110011 01101001 01101110 00100111 01110011 00100000 01000011 01110010 01100101 01100101 01100100 00100000 00110011 00101110" | awk -f translateBinary.awk

Like & comment "AC3" for a chance to win a FREE download of Assassin's Creed 3.
[root@expressdb1 ~]#

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.