-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_decode.sh
More file actions
84 lines (61 loc) · 1.67 KB
/
Copy pathcode_decode.sh
File metadata and controls
84 lines (61 loc) · 1.67 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#/bin/bash
#code
for i in $1; do
array+=($i)
done
for ((j=0; j<${#array[@]}; j++)); do
while read -n 1 char; do
ArrChar+=($char) #array of char
done <<< "${array[$j]}"
done
for ((i=0; i<${#ArrChar[@]}; i++)); do
bin=$(echo "obase=2;${ArrChar[$i]}" | bc) #decimal to binary
k=${#bin} #number of bits in char
for ((j=0; j<$(($k-1)); j++)); do
num="0" #writing 0 (k-1) times
done
num="$num""1" #after 0 always 1
num="$num""$bin" #after 0....1 always initial number
Result+=($num) #unite our code numbers in array Result
unset num
done
for ((i=0; i<${#Result[@]}; i++)); do
echo -n ${Result[$i]}
done
echo
unset ArrChar
#this was code, after will decode
for i in $2; do
Array+=($i)
done
for ((j=0; j<${#Array[@]}; j++)); do
while read -n 1 char; do
ArrChar+=($char) #Array of char
done <<< "${Array[$j]}"
done
pos=0
for ((i=0; i<${#ArrChar[@]}; )); do
length=1 #minimal length is 1
while ((${ArrChar[$i]}!="1")); do
length=$(($length+1)) #determine length with first 1
i=$(($i+1))
done
i=$(($i+1))
for ((j=$length; j<$((2*$length)); j++)); do
num="$num""${ArrChar[$i]}" #determine binary namber that we use in decode number
i=$(($i+1))
done
ArrBinNum+=($num) #create array of binary numbers that we will use in decode number
unset num
unset length
done
for ((i=0;i<${#ArrBinNum[@]};i++)); do
dec=$(echo "ibase=2;${ArrBinNum[$i]}" | bc) #binary in decimal
ArrDecNum+=($dec) #create array of decimal nambers that using in decode result
unset dec
done
for ((j=0;j<${#ArrDecNum[@]};j++)); do
echo -n ${ArrDecNum[$j]}
done
echo
exit 0