Quick ASCII Binary Tools

Just going to leave this here (and here) in case anyone needs a quick set of source-able functions to make quick ASCII->Binary and Binary->ASCII conversions from Linux command lines. 🙂

#!/bin/bash
# Helper functions to allow quick conversion back and forth between binary and ASCII
# Derived from http://unix.stackexchange.com/questions/98948/ascii-binary-tools
# Last modified 2018-06-19 
# Author: Hermit
  
chrbin() {
     echo $(printf \\$(echo "ibase=2; obase=8; $1" | bc))
}
 
ordbin() {
     a=$(printf '%d' "'$1")
     echo "obase=2; $a" | bc
}
 
ascii2bin() {
     echo -n $* | while IFS= read -r -n1 char
     do
          ordbin $char | tr -d '\n'
          echo -n " "
     done
}
 
bin2ascii() {
     for bin in $*; do
          chrbin $bin | tr -d '\n'
     done
     echo
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

Website Powered by WordPress.com.

Up ↑

%d bloggers like this: