-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdecimal_binary.sh
executable file
·75 lines (62 loc) · 1.84 KB
/
decimal_binary.sh
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
#!/usr/bin/env bash
# Script Name: decimal_binary
# Description: Converts decimal numbers to their binary representation and vice versa.
# Usage: DecimalBinaryConverter.sh [-d2b|-b2d] [number]
# [-d2b|-b2d] - Flag to specify the conversion direction (-d2b for decimal to binary, -b2d for binary to decimal).
# [number] - The number to be converted.
# Example: ./decimal_binary.sh -d2b 123
# ./decimal_binary.sh -b2d 1111011
# Print usage function
print_usage() {
echo "Usage: $0 [-d2b|-b2b] [number]"
echo "Converts decimal numbers to their binary representation and vice versa."
}
# Validation of input function
validate_input() {
re='^[0-9]+$'
if ! [[ $1 =~ $re ]]; then
echo "Error: Input is not a positive integer!"
exit 1
fi
}
# Conversion of decimal to binary function
decimal_to_binary() {
local number=$1
local remainder
local binary_representation=""
while [ "$number" -gt 0 ]
do
remainder=$(( number % 2))
binary_representation="$remainder$binary_representation"
number=$(( number / 2))
done
echo "$binary_representation"
}
# Conversion of binary to decimal function
binary_to_decimal() {
local binary=$1
echo "$((2#$binary))"
}
# Main function
main() {
if [ $# -ne 2 ]; then
echo "Error: Must provide exactly two arguments!"
print_usage
exit 1
fi
local operation=$1
local number=$2
validate_input "$number"
if [ "$operation" == "-d2b" ]; then
echo "Conversion of decimal number $number to binary:"
decimal_to_binary "$number"
elif [ "$operation" == "-b2d" ]; then
echo "Conversion of binary number $number to decimal:"
binary_to_decimal "$number"
else
echo "Error: Invalid operation $operation!"
print_usage
exit 1
fi
}
main "$@"