Shell Script to Convert Binary to Decimal
#!/bin/bash
# Take input as binary number
echo “Enter Binary Number: “
read n# Validate input
if [[ ! “$n” =~ ^[01]+$ ]]; then
echo “Error: Invalid binary number.”
exit 1
fi# Initialize variables
i=0
num=0# Convert binary to decimal
while [ $n -ne 0 ]; do
digit=$((n % 10))
num=$((num + digit * 2**i))
n=$((n / 10))
let “i++”
done# Print the result
echo “Resultant Decimal Number: $num”
Step by Step Explanation of the Shell Script to Convert Binary to Decimal
1. Setting up the script:
#!/bin/bash
: This tells the system to use the bash interpreter to run the script.echo "Enter Binary Number: "
: This displays a message asking the user to enter a binary number.read n
: This reads the user’s input and stores it in the variablen
.
2. Validating the input:
if [[ ! "<span class="math-inline">n" \=\~ ^\[01\]\+</span> ]]; then
: This checks if the inputn
contains only 0s and 1s using regular expressions.!
: This negates the expression, meaning it checks for the absence of valid characters.^
: This matches the beginning of the string.[01]+
: This matches one or more repetitions of 0s or 1s.$
: This matches the end of the string.
echo "Error: Invalid binary number."
: If the input is invalid, this displays an error message.exit 1
: This exits the script with an error code of 1.
3. Initializing variables:
i=0
: This initializes the variablei
to 0. This variable will be used as a counter later.num=0
: This initializes the variablenum
to 0. This variable will store the final decimal number.
4. Converting binary to decimal:
while [ $n -ne 0 ]; do
: This starts a loop that continues until the value ofn
becomes 0.<span class="math-inline">n \-ne 0\
: This checks if `n` is not equal to 0. * `digit=((n % 10)): This calculates the rightmost digit of the binary number stored in
nand assigns it to the variable
digit`.%
: This is the modulus operator which gives the remainder of dividingn
by 10.<span class="math-inline">\(\(\.\.\.\)\)\
: This is a bash expression used for arithmetic operations. * `num=((num + digit * 2**i)): This adds the value of
digitmultiplied by 2 raised to the power of
ito the variable
num`.2**i
: This raises 2 to the power ofi
.+
: This adds the two values.
n=$((n / 10))
: This removes the rightmost digit from the binary number stored inn
./
: This is the division operator.
let "i++"
: This increments the value of the variablei
by 1.done
: This ends the loop.
5. Printing the result:
echo "Resultant Decimal Number: $num"
: This displays the final decimal number stored in the variablenum
.
How to execute the bash files?
Write the bash code into a text file using text editors. In this example we are using Vim text editor and saving our file with `.sh` extension.
Open terminal and execute the file using below command:
vim Binary.sh
Making Script Executable
chmod +x Binary.sh
Execute the Script
./Binary.sh
Output:
Shell script to convert binary to decimal
Binary and decimal are two fundamental number systems used in computing. While humans typically interact with decimal numbers (0-9), computers internally store and process data using binary digits (0s and 1s). Converting between these systems is often necessary when working with computer programs or manipulating data. This article explores how to write a shell script that converts a binary number to its decimal equivalent.
Understanding the Conversion Process
Converting a binary number to decimal involves multiplying each binary digit by its corresponding power of 2, starting from the rightmost digit as the exponent and increasing by 1 for each subsequent digit to the left. Finally, we sum the products of these individual multiplications to obtain the decimal equivalent.
Given a binary number as input, we need to make a conversion from a binary number to its equivalent decimal number.
Examples:
Input : 1011
Output : 11
Input : 1111
Output : 15
Binary to decimal
The idea is to retain the decimal number from the binary number, so, take the binary number and traverse from right to left, taking one by one the last digit of the binary number.
Pick the last digit and multiply by the proper base (power of two) and add it to the final variable (say num).
Repeat this process until the binary number is not equal to zero and then print the result (that is, num).
For example, let’s consider the binary number
Example 1 :
n = 10101 (Binary number)
- Rightmost digit (1) * 2^0 = 1
- Second digit (0) * 2^1 = 0
- Third digit (1) * 2^2 = 4
- Fourth digit (0) * 2^3 = 0
- Leftmost digit (1) * 2^4 = 16
Summing these values: 1 + 0 + 4 + 0 + 16 = 21
Therefore, the decimal equivalent of 10101 is 21.
Example 2:
n = 101 (Binary number)
num = 1*(2^2) + 0*(2^1) + 1*(2^0) = 6 (resultant decimal)
Example 3:
n = 1101 (Binary number)
num =1*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0) = 13 (resultant decimal)
Here We will extract the decimal number from the given binary number.