Skip to content

Instantly share code, notes, and snippets.

@pfichtner
Last active June 24, 2017 13:36
Show Gist options
  • Select an option

  • Save pfichtner/2f99ed4503a63f0e970aab7ec57742be to your computer and use it in GitHub Desktop.

Select an option

Save pfichtner/2f99ed4503a63f0e970aab7ec57742be to your computer and use it in GitHub Desktop.
Converting TXP to binary and vice versa using shell utilities only
#! /bin/sh
txpToBin() {
# filter "1 or 3 or ,"
# insert linebreak after every second ","
# remove comma
# append "3" if missing (this can only match the last line)
# translate 13->0 and 31->1
# join all lines to one string
echo $TXP | sed 's/[^1,3,\,]*//g' | sed -e's/\(\([^,]*,\)\{1\}[^,]*\),/\1\n/g' | tr -d ',' | sed ':a;/.\{2\}/!{s/$/3/;ba}' | sed 's/13/0/g;s/31/1/g' | sed ':a;N;$!ba;s/\n//g'
}
binToTxp() {
# filter "0 or 1"
# translate 1->X and 0->-
# translate X->31 and -->13
# insert comma after each character
echo "$BIN" | sed 's/[^0-1]*//g' | sed 's/1/X/g;s/0/-/g' | sed 's/X/31/g;s/-/13/g' | sed ':a;s/\B[0-9]\>/,&/;ta'
}
# convert a TXP string into its binary representation (odd length)
TXP="3,1,3,1,1,3,3,1,1,3,3,1,1,3,3,1,1,3,3,1,1,3,3,1,3,1,3,1,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,3,1,3,1,1"
BIN=`txpToBin`
echo "$TXP"
echo "$BIN"
echo
# convert a TXP string into its binary representation (even length)
TXP="$TXP,3"
BIN=`txpToBin`
echo "$TXP"
echo "$BIN"
echo
# convert a binary string into its TXP representation
TXP=`binToTxp`
echo "$BIN"
echo "$TXP"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment