Skip to content

Commit

Permalink
added more comments, consolidated some lines of code
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Hurley committed Feb 25, 2024
1 parent f4d02e9 commit 31005ae
Showing 1 changed file with 28 additions and 47 deletions.
75 changes: 28 additions & 47 deletions bin/NumberTea
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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 )
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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"

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
;;

Expand Down

0 comments on commit 31005ae

Please sign in to comment.