Linux Convert Text To Binary & Back Again

View previous topic View next topic Go down

Linux Convert Text To Binary & Back Again

Post by jamied_uk on 30th July 2016, 14:07




Text To Binary


Code:
#!/bin/bash
# Usage: ./txt_to_binary2.sh "test123"
myvar="$@"
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
}
ascii2bin "$myvar"







Binary To Text


Code:

#!/bin/bash
# Usage: ./binary_to_txt.sh "1010100 1001000 1001001 1010011 0 1001001 1010011 0 1000010 1001001 1001110 1000001 1010010 1011001 0 1100010 1101100 1100001 1100010 1101100 1100001 1100010 1101100 1100001 1100010 1101100 1100001 1100010 1101100 1100001 1100010 1101100 1100001 0 110001 110010 110011 110100 110101 110110 110111 111000 111001 110000"

myvar="$@"
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
}
#ascii2bin "$myvar"

# Convert Binary To Text!
#
bin2ascii "$myvar"
avatar
jamied_uk
Admin

Posts : 2260
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum