From 31005ae9161e821b7e69cd5843883eb6b0059f12 Mon Sep 17 00:00:00 2001 From: Patrick Hurley <> Date: Sun, 25 Feb 2024 01:33:56 -0500 Subject: [PATCH] added more comments, consolidated some lines of code --- bin/NumberTea | 75 +++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 47 deletions(-) diff --git a/bin/NumberTea b/bin/NumberTea index f7ce554..7b01d6a 100644 --- a/bin/NumberTea +++ b/bin/NumberTea @@ -25,7 +25,7 @@ OPTIONS i.e. \$ ./$(basename $0) --dec 123 dec 123 -bin 01111011 +bin 1111011 hex 7B \$ ./$(basename $0) -d 123 -b 101000001 -h FFF @@ -43,10 +43,10 @@ hex FFF \$ ./$(basename $0) -t \"hello there\" encoded: hello there -decoded: 01101000 01100101 01101100 01101100 01101111 00100000 01110100 01101000 01100101 01110010 01100101 +decoded: 0110100001100101011011000110110001101111001000000111010001101000011001010111001001100101 -\$ ./$(basename $0) --text 0110100001100101011011000110110001101111001000000111010001101000011001010111001001100101 -encoded: 0110100001100101011011000110110001101111001000000111010001101000011001010111001001100101 +\$ ./$(basename $0) --text \"01101000 01100101 01101100 01101100 01101111 00100000 01110100 01101000 01100101 01110010 01100101\" +encoded: 01101000 01100101 01101100 01101100 01101111 00100000 01110100 01101000 01100101 01110010 01100101 decoded: hello there \$ ./$(basename $0) -f textFile @@ -55,16 +55,14 @@ Encoded file: encoded_textFile Decoded file: decoded_binFile\n" } -toBin() { - local value=$(echo "${1^^}") +toBin() { # arg1: a numeric value, arg2: value's numbering system. Echos the binary representation of the input value. + local value=$(echo "${1^^}") # hex_to_bin array is in UPPERCASE local type="$2" case "$type" in - bin ) - echo $value - ;; + bin) echo $value ;; - dec ) + dec) while [ $value -gt 0 ]; do local remainder=$(( $value % 2 )) local binary="$remainder$binary" @@ -74,7 +72,7 @@ toBin() { echo $binary ;; - hex ) + hex) local -A hex_to_bin=( [0]=0000 [1]=0001 [2]=0010 [3]=0011 [4]=0100 [5]=0101 [6]=0110 [7]=0111 [8]=1000 [9]=1001 [A]=1010 [B]=1011 [C]=1100 [D]=1101 [E]=1110 [F]=1111 @@ -83,20 +81,19 @@ toBin() { for ((i = 0; i < ${#value}; i++)); do local binary+="${hex_to_bin[${value:i:1}]}" done - # Remove leading zeros - local binary=$(echo $binary | sed 's/^0*//') + local binary=$(echo $binary | sed 's/^0*//') # Remove leading zeros echo $binary ;; esac } -toDec() { - local value=$(echo "${1^^}") +toDec() { # arg1: a numeric value, arg2: value's numbering system. Echos the decimal representation of the input value. + local value=$(echo "${1^^}") # hex_to_dec array is in UPPERCASE local type="$2" case "$type" in - bin ) + bin) local decimal=0 local position=1 @@ -109,11 +106,9 @@ toDec() { echo $decimal ;; - dec ) - echo $value - ;; + dec) echo $value ;; - hex ) + hex) #local decimal=$(toDec $(toBin $value "hex") "bin") local decimal=0 local -A hex_to_dec=( [A]=10 [B]=11 [C]=12 [D]=13 [E]=14 [F]=15 ) @@ -132,12 +127,12 @@ toDec() { esac } -toHex() { - local value=$(echo "${1^^}") +toHex() { # arg1: a numeric value, arg2: value's numbering system. Echos the hexadecimal representation of the input value. + local value=$(echo "${1^^}") # bin_to_hex array is in UPPERCASE local type="$2" case "$type" in - bin ) + bin) local -A bin_to_hex=( [0000]=0 [0001]=1 [0010]=2 [0011]=3 [0100]=4 [0101]=5 [0110]=6 [0111]=7 [1000]=8 [1001]=9 [1010]=A [1011]=B [1100]=C [1101]=D [1110]=E [1111]=F @@ -161,7 +156,7 @@ toHex() { echo $hexadecimal ;; - dec ) + dec) #local hexadecimal=$(toHex $(toBin $value "dec") "bin") while [[ $value -gt 0 ]]; do local remainder=$((value % 16)) @@ -183,13 +178,11 @@ toHex() { echo $hexadecimal ;; - hex ) - echo $value - ;; + hex) echo $value ;; esac } -textTea() { +textTea() { # arg1: a numeric value, arg2: value's numbering system. Only works in BIN or DEC, evaluates ASCII value not numerical value. local value="$1" local type="$2" @@ -248,12 +241,12 @@ textTea() { esac } -runFile() { +runFile() { # arg1: path to a text file of BIN or TXT. Determins if file is striclty BIN, otherwise treated as text. local filePath="$1" local fileName=$(basename "$filePath") local nonBin=0 - while IFS= read -r -n1 char; do + while IFS= read -r -n1 char; do # If there are any non-binary char (excluding space, newline, EOF) indicate file as NON-BINARY. [[ ! $char =~ ^[01\ \\n]+$ && ! -z $char ]] && nonBin=$(($nonBin + 1)) done < $filePath @@ -286,46 +279,34 @@ while [[ "$1" =~ ^-.* ]]; do -b | --bin) shift [[ ! $1 =~ ^[01]+$ ]] && printf "$1 is not binary.\n\n" && shift && continue - bin=$1 - dec=$(toDec $1 "bin") - hex=$(toHex $1 "bin") - + bin=$1; dec=$(toDec $1 "bin"); hex=$(toHex $1 "bin") printf "dec ${dec}\nbin ${bin}\nhex ${hex}\n\n" ;; -d | --dec) shift [[ ! $1 =~ ^[0-9]+$ ]] && printf "$1 is not decimal.\n\n" && shift && continue - bin=$(toBin $1 "dec") - dec=$1 - hex=$(toHex $1 "dec") - + bin=$(toBin $1 "dec"); dec=$1; hex=$(toHex $1 "dec") printf "dec ${dec}\nbin ${bin}\nhex ${hex}\n\n" ;; -h | --hex) shift [[ ! $1 =~ ^[0-9a-fA-F]+$ ]] && printf "$1 is not hexadecimal.\n\n" && shift && continue - bin=$(toBin $1 "hex") - dec=$(toDec $1 "hex") - hex=$(toHex $1 "hex") - + bin=$(toBin $1 "hex"); dec=$(toDec $1 "hex"); hex=$(toHex $1 "hex") printf "dec ${dec}\nbin ${bin}\nhex ${hex}\n\n" ;; -t | --text) shift encoded="$1" - if [[ $1 =~ ^[01\ ]+$ ]]; then - decoded=$(textTea "$1" "bin") - else - decoded=$(textTea "$1" "txt") - fi + [[ $1 =~ ^[01\ ]+$ ]] && decoded=$(textTea "$1" "bin") || decoded=$(textTea "$1" "txt") printf "encoded: ${encoded}\ndecoded: ${decoded}\n\n" ;; -f | --file) shift [[ ! -f $1 ]] && printf "Cannot read file $1.\n\n" && shift && continue + runFile "$1" ;;