-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_decode.sh
More file actions
56 lines (56 loc) · 1.09 KB
/
Copy pathcode_decode.sh
File metadata and controls
56 lines (56 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#/bin/bash
function code {
for ((i=0; i<N; i++)); do
if ((${number:$i:1}==0)); then
printf "10"
elif ((${number:$i:1}==1)); then
printf "11"
else
new=${number:$i:1}
while ((new>1)); do #degree of number
let new="$new/2"
printf "0"
done
printf "1"
printf "$(echo "obase=2;${number:$i:1}" | bc)" #convert to binary
fi
done
return
}
function decode {
i=$1
if ((i>=N)); then #end of number condition
echo
exit 0
else
size=0
a=${number:$i:1}
while ((a!="1")); do #determining the number of bits
let i="$i+1"
let size="$size+1"
a=${number:$i:1}
done
let i="$i+1" #position shift by 1
let size="$size+1"
for ((j=0; j<size; j++)); do #number reading
chislo=$chislo${number:$i:1}
let i="$i+1"
done
echo -n $((2#$chislo)) #convert to dec
chislo=""
decode $i #recursive for each number
fi
return
}
read number
N=${#number} #size of number
if (($1=="1")); then
code
fi
if (($1=="2")); then
decode 0
else
printf "ERROR OPERATION"
fi
echo
exit 0